簡體   English   中英

預加載異步圖像以離線顯示在UIimageview中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM