SDWebImage : setImageWithURL fails when used to se

2020-05-23 04:30发布

问题:

When call setImageWithURL, it fails with following,

[UIImageView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance 0x149a20 2011-12-14 18:11:38.060 [781:707] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance 0x149a20'

I can confirm I have included SDWebImage project and required headers correctly as I can use the SDWebImageManager successfully.

Following is the code where I called the UIImageView category method setImageWithURL

NSURL* url = [NSURL URLWithString:@"www.abc.com/abc.png"];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"abc.png"]];    

回答1:

I ended up inserting all the .h and .m files into my project and then it worked fine. I got the same error when I only included the .a and .h files.



回答2:

As per: linking objective-c categories in a static library

Due to a linker bug in XCode, you need to set the 'Other Linker Flags' setting your build target. Setting the flag as follows should eliminate this error.

-force_load ${BUILT_PRODUCTS_DIR}/libSDWebImage.a


回答3:

This will solve the problem: Open the "Build Settings" tab, in the "Linking" section, locate the "Other Linker Flags" setting and add the "-ObjC" flag:



回答4:

If anyone, like me, still has problems after seeing all the other answers here (e.g. force_load or load_all) even after following all the proper installation instructions, here is what I have learnt after doing some search online:

  • This problem only happens when you use iPhone 5S or the 64-bit simulator
  • This is a problem with libwebp

The solution to this problem: Only do this if you don't need the 64-bit processor for your app (too new to iOS programming to know if this sentence is valid)

  1. Go to build settings > Architecture

  2. Under Architecture, select Standard architectures (armv7,armv7s)

  3. Under Build Active Architecture Only, make sure it is Yes for both Debug and Release

My source: https://github.com/rs/SDWebImage/issues/494

I hope this helps those who, like me, found this while trying to solve your problem =)



回答5:

In the SDWebImage docs, it says that you have to set "Other Linker Flags" to -ObjC. This did not work for me. Instead, I set it to -all_load. I also had to remove -ObjC. The explanation for what is going on can be found here:

http://developer.apple.com/library/mac/#qa/qa1490/_index.html

It also explains why -ObjC fails: "Important: For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the f-all_load or -force_load flags"

Using -all_load increases the size of the executable. You can use -force_load to mitigate this somewhat. For SDWebImage 2.7.3, I did this by setting the following in "Other Linker Flags" in the Build Settings: "-load_all $(SRCROOT)/SDWebImage.framework/SDWebImage". Linking to libSDWebImage[ARC].a which is what some sources advise, didn't work for me.

Incidentally, in Xcode 4.4, it seems you have to do a slow double-click in order to change build settings properly. Doing a normal double-click shows the completely screwed up and non-working pop-up.



回答6:

Solved the same problem adding the libSDWebImage.a to Build Phases > Link Binary with Libraries.



回答7:

If you read the document here https://github.com/rs/SDWebImage at the bottom there is a download link with the CORRECT files to download. Following that example it works perfectly.

The mistake I made, and seemingly others to, is I downloaded the github project and tried using that.



回答8:

Everything started to work for me once I linked "ImageIO.framework" to my main project.



回答9:

You can also set 'Other Linker Flags' to -all_load. This solves the problem where an external framework (such as the filepicker framework) requires linking against SDWebImage.



回答10:

-force_load is not fix fundamental problem (when achiveing problem happens) to fix this linking problem..

  1. edit scheme for SDWebImage,
  2. run scheme set for release
  3. run (with device)
  4. libSDWebImage.a will be generated in release-iphoneos directory.
  5. set target->BuildPhase->link Binarywithlibrary .. add this lib.
  6. Done..


回答11:

Make sure your compiled files have UIIMageView+AFNetworking.m in case you're using AFNetworking framework.



回答12:

You need to add the libSDWebImage.a in your linked binaries, also check for other linker flags, it should be -ObjC



回答13:

Checkout your choice when you pull files of SDWebimages into Xcode, do mark the "create groups" in added folders, and do not mark the "create folder references".

Mark exactly as the picture: