更新时间:2023-12-05 16:02:10
SDWebImage文档表示它会自动缓存.至于缩放,有很多的净代码像这样大约.诀窍是将其与SDWebImage集成.幸运的是,它提供了一个完成块:
SDWebImage docs says it caches automatically. As for scaling, there's plenty of net code like this around. The trick is integrating it with SDWebImage. Fortunately, it provides a completion block:
NSURL *url = /* prepare a url... see note below */
[myCell.homeImage setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"menuButton.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
// inspect cacheType here to make sure it's cached as you want it
myCell.homeImage.image = [self scaleImage:image toSize:CGSizeMake(75,75)];
}];
一个简单的标尺看起来像这样(未经测试):
A simple scale would look something like this (not tested):
- (UIImage *)scale:(UIImage *)image toSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
偶然地,我注意到您使用通用字符串操作来构建URL.您***对字符串使用专门的方法,如下所示:
Incidentally, I notice that you use generic string manipulation to build the URL. You'd be better off using the specialized methods on string as follows:
// this can be defined outside cellForRowAtIndexPath
NSURL *baseUrl = [NSURL urlWithString:@"http://example.com/"];
NSString *imageItemName = [homeImages objectAtIndex:row];
NSURL *url = [NSURL URLWithString:imageItemName relativeToURL:baseURL];