[英]Getting image from my dictionary is causing an "unrecognized selector" error
我正在 iOS 中解析 JSON 文件,並且在 tableview 中獲取元素,但是當我創建 ( prepareForSegue
) 以獲取 tableview 元素時,我無法獲取圖像,並且出現此錯誤:
無法識別的選擇器發送到實例 0x7fa3427c5b20 2016-05-05 05:08:43.287 MovieClub[4845:212195] *** 由於未捕獲的異常“NSInvalidArgumentException”而終止應用程序,原因:“-[__NSCF30c5b20c20c5c50c20c07 發送到 unrecogn20cfc20c07 選擇器大小] '
在這行代碼中:
self.postt.image =[self.actuatlitDetail objectForKey:@"poster_240x342"];
如果[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;
});
});
它的 url 值 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;
希望它有幫助
設置圖像路徑然后按照這個:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"Your image url string"]];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.