In Xcode 5.0.2
- I create a blank single view app for iPhone,
- then add a "male.png" image to the project,
- drag a UIImageView to the storyboard
and finally add the following code to the
viewDidLoad
:_imageView.image = [UIImage imageNamed:@"male.png"];
This works well:
Then I add the 4 files from JMImageCache project and change the ViewController.m to:
#import "ViewController.h"
#import "JMImageCache.h"
static NSString* const kAvatar = @"http://gravatar.com/avatar/55b3816622d935e50098bb44c17663bc.png";
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[_imageView setImageWithURL:[NSURL URLWithString:kAvatar]
placeholder:[UIImage imageNamed:@"male.png"]];
}
@end
Unfortunately, this results in app crash with the error message Thread 1: EXC_BAD_ACCESS
:
At his webpage Jake Marsh (the author of JMImageCache) notes:
JMImageCache purposefully uses NSString objects instead of NSURL's to make things easier and cut down on [NSURL URLWithString:@"..."] bits everywhere. Just something to notice in case you see any strange EXC_BAD_ACCESS exceptions, make sure you're passing in NSString's and not NSURL's.
But (as an iOS programming newbie) I don't understand, what exactly does Mr. Marsh mean - since his file UIImageView+JMImageCache.m declares the 1st argument for the public method as NSURL
:
- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage {
[self setImageWithURL:url key:nil placeholder:placeholderImage];
}
Is the note maybe outdated and how could I fix my app?
That's a bug in JMImageCache.
setImageWithURL:key:placeholder:completionBlock:
calls itself, exhausting the stack.To work around the bug, call the longer form of the method:
Or, use an older version of the library (e.g. 0.4.0). Looks like the bug was introduced in 1af09be78a.