从我的字典中获取图像会导致“无法识别的选择器”错误

时间:2016-05-05 04:35:30

标签: ios objective-c json uitableview unrecognized-selector

我在iOS中解析JSON文件,我在tableview中获取元素,但是当我创建(prepareForSegue)获取tableview元素时,我无法获取图像,我收到此错误:

  

无法识别的选择器发送到实例0x7fa3427c5b20 2016-05-05 05:08:43.287 MovieClub [4845:212195] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFString size]:无法识别的选择器已发送例如0x7fa3427c5b20'

在这行代码中:

self.postt.image =[self.actuatlitDetail objectForKey:@"poster_240x342"];

3 个答案:

答案 0 :(得分:1)

如果[self.actuatlitDetail objectForKey:@"poster_240x342"]返回NSURL,您可以使用

NSURL *imageURL = [self.actuatlitDetail objectForKey:@"poster_240x342"];
self.postt.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

如果是NSString则表示URL

NSString *imageURLString = [self.actuatlitDetail objectForKey:@"poster_240x342"];
self.postt.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURLString]]];

您可以在后台加载图片

__weak typeof(self) weakSelf = self;
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(aQueue, ^{
    NSURL *imageURL = [weakSelf.actuatlitDetail objectForKey:@"poster_240x342"];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
    dispatch_async(dispatch_get_main_queue(), ^{
        weakSelf.postt.image = image;
    });
});

答案 1 :(得分:0)

它的urt值为actuatlitDetail,得到以下代码,

NSString *imageurl = [self.actuatlitDetail objectForKey:@"poster_240x342"];
NSString *encodedUrlString = [imageurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageURL = [NSURL URLWithString: encodedUrlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imag = [UIImage imageWithData:imageData];
self.postt.image = imag;

希望它有用

答案 2 :(得分:0)

设置图像路径然后按照:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"Your image url string"]];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];