如何异步加载图像?这是我正在做的事情(借用开源代码加载图像缩略图):
- (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView
imageAtIndex:(NSInteger)index
{
NSURL* url = [NSURL URLWithString:[recommendedArray objectAtIndex:index]] ;
// [[cell grid_image] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
BOOL isImageLoaded = YES;
UIImage *bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[recommendedArray objectAtIndex:index] lastPathComponent]]];
if(bookImage == nil)
isImageLoaded = NO;
if(!isImageLoaded){
UIImage *thumbnailImage =[UIImage imageNamed:@"App-icon-144x144.png"];}
else{
UIImage *thumbnailImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[recommendedArray objectAtIndex:index]]]]];
return thumbnailImage;
}
}
我尝试将以下内容添加到方法中,但它为NSURL产生错误:
dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.g.load", NULL);
dispatch_async(imageLoadQueue, ^{
//Wait for 5 seconds...
usleep(1000000);
NSArray *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
for(int k=0; k < [images count];k++){
NSLog(@"Loading %d Image", k);
NSString *imgURL = [images objectAtIndex:k];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgURL]];
[imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];
}
[[self collectionView] reloadData];
NSLog(@"DONE WITH ALL IMAGES");
});
错误
[NSURL length]: unrecognized selector sent to instance 0x12ea7240
2013-04-18 11:18:35.456 AssamKart[3084:14d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0x12ea7240'
答案 0 :(得分:0)
方法内 - &gt;
- (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView
imageAtIndex:(NSInteger)index
使用此 - &gt;
NSURLConnection *aNSURLConnection= [[NSURLConnection alloc]initWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#> startImmediately:<#(BOOL)#>];
和代表内部
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
获取图像数据并实现您的逻辑,此处您将再次调用
- (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView
imageAtIndex:(NSInteger)index
方法递归,直到recommendedArray
用尽。
答案 1 :(得分:0)
就这样做,
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int k=0; k<[images count]; k++) {
NSString *imgURL = [images objectAtIndex:k];
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:imgURL]];
[data writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];
}
});
答案 2 :(得分:0)
您可以使用SDWebImage从互联网下载图片并使用以下方法:
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
答案 3 :(得分:0)
使用AsyncImageView。
https://github.com/nicklockwood/AsyncImageView
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(Your Frame)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:URL]];
[AsyncImageLoader defaultCache];
[[AsyncImageLoader sharedLoader]loadImageWithURL:URL target:self success:@selector(successSelector) failure:@selector(failureSelector)];