[英]Preload Asynchronous Image to show in UIimageview offline
我编写应用程序以使用URL中的图片在UIimageview中显示,但是我在iPhone imageview中运行,不显示图片,这是我的代码
__block UIImage *pic1,*pic2,*pic3;
UIImageView *p1,*p2,*p3;
NSString *strImgURLAsString = mylink;
NSString *strImgURLAsString2 = mylink2;
[strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imgURL = [NSURL URLWithString:strImgURLAsString];
[strImgURLAsString2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imgURL = [NSURL URLWithString:strImgURLAsString];
NSURL *imgURL2 = [NSURL URLWithString:strImgURLAsString2];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"downloaded");
pic1 = [[UIImage alloc] initWithData:data];
}];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"downloaded");
pic2 = [[UIImage alloc] initWithData:data];
}];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"downloaded");
pic1 = [[UIImage alloc] initWithData:data];
}];
if([name isEqualToString:@"des1"])// it is restoration of viewcontroller{
[p1 setImage:pic1];
}
else if ([name isEqualToString:@"des2"])// it is restoration of viewcontroller{
[p1 setImage:pic2];
}
您已设置图像,然后在块中下载设置图像,如下所示
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"downloaded");
dispatch_async(dispatch_get_main_queue(), ^{
pic1 = [[UIImage alloc] initWithData:data];
if([name isEqualToString:@"des1"]){
[p1 setImage:pic1];
});
}
}];
dispatch_async(dispatch_get_main_queue(), ^{ }) use for load image on main thread because u download image asynchronous
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.