您好我正在尝试将图像从描述显示到自定义UIViewCell,但问题是缩略图返回null!我无法在此处显示任何图片,我的代码基于THIS QUESTION:
// Configure the cell.
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]";
cell.title.text = itemTitle;
NSMutableString *subtitle = [NSMutableString string];
[subtitle appendString:itemSummary];
NSString *htmlContent = item.content;
NSString *imgSrc;
// find match for image
NSRange rangeOfString = NSMakeRange(0, [htmlContent length]);
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(<img.*?src=\")(.*?)(\".*?>)" options:0 error:nil];
if ([htmlContent length] > 0) {
NSTextCheckingResult *match = [regex firstMatchInString:htmlContent options:0 range:rangeOfString];
if (match != NULL ) {
NSString *imgUrl = [htmlContent substringWithRange:[match rangeAtIndex:2]];
NSLog(@"url: %@", imgUrl);
NSLog(@"match %@", match);
if ([[imgUrl lowercaseString] rangeOfString:@"feedburner"].location == NSNotFound) {
imgSrc = imgUrl;
}
}
}
[cell.thumbnails setImageWithURL:[NSURL URLWithString:imgSrc]
placeholderImage:[UIImage imageNamed:@"thumb.png"]];
}
问题是item.thumbnail返回null!
答案 0 :(得分:0)
我用这段代码解决了这个问题:
//image
NSString *imageurl = nil;
NSScanner *theScanner = [NSScanner scannerWithString:item.summary];
[theScanner scanUpToString:@"<img" intoString:nil];
if (![theScanner isAtEnd]) {
[theScanner scanUpToString:@"src" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
[theScanner scanUpToCharactersFromSet:charset intoString:nil];
[theScanner scanCharactersFromSet:charset intoString:nil];
[theScanner scanUpToCharactersFromSet:charset intoString:&imageurl];
imageurl = [imageurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([imageurl rangeOfString:@"mf.gif"].location != NSNotFound || [imageurl rangeOfString:@"sayyac"].location != NSNotFound || [imageurl rangeOfString:@"feedburner"].location != NSNotFound) {
imageurl = @"false";
}
} else {
// resim yoksa
imageurl = @"false";
}
[cell.thumbnails setImageWithURL:[NSURL URLWithString:imageurl]
placeholderImage:[UIImage imageNamed:@"thumb.png"]];
}