JMImageCache in a simple test app crashes with EXC

2019-06-14 04:43发布

In Xcode 5.0.2

  1. I create a blank single view app for iPhone,
  2. then add a "male.png" image to the project,
  3. drag a UIImageView to the storyboard
  4. and finally add the following code to the viewDidLoad:

    _imageView.image = [UIImage imageNamed:@"male.png"];

This works well:

enter image description here

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:

crash

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?

1条回答
趁早两清
2楼-- · 2019-06-14 05:36

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:

[_imageView setImageWithURL:[NSURL URLWithString:kAvatar]
    key:nil
    placeholder:[UIImage imageNamed:@"male.png"]
    completionBlock:nil
    failureBlock:nil];

Or, use an older version of the library (e.g. 0.4.0). Looks like the bug was introduced in 1af09be78a.

查看更多
登录 后发表回答