简体   繁体   English

花时间将图像从URL加载到UIImageview

[英]Taking time to load the image from URL to UIImageview

I am using this code for displaying the image from URL to UIImageview 我使用此代码显示从URL到UIImageview的图像

UIImageView *myview=[[UIImageView alloc]init];

myview.frame = CGRectMake(50, 50, 320, 480);

NSURL *imgURL=[[NSURL alloc]initWithString:@"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];

NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];

UIImage *image=[[UIImage alloc]initWithData:imgdata];

myview.image=image;

[self.view addSubview:myview];

But the problem is that its taking too long time to display the image in imageview. 但问题是它需要花费太长时间才能在imageview中显示图像。

Please help me... 请帮我...

Is there any method to fast the process... 有没有办法加快这个过程......

Instead of dispatch_async, Use SDWebImage for caching the images. 而不是dispatch_async,使用SDWebImage缓存图像。

This is best I have seen... 这是我见过的最好的......

The problem of dispatch_async is that if you lost focus from image, it will load again. dispatch_async的问题在于,如果您从图像中失去焦点,它将再次加载。 However SDWebImage, Caches the image and it wont reload again. 但SDWebImage,缓存图像,它不会再次重新加载。

Use Dispatch queue to load image from URL.


dispatch_async(dispatch_get_main_queue(), ^{

  });

Or add a placeholder image till your image gets load from URL.

The answers given to me on my own question Understanding the behaviour of [NSData dataWithContentsOfURL:URL] inside the GCD block does makes sense.So be sure that if you use [NSData dataWithContentsOfURL:URL] inside the GCD(as many developers do these days) is not a great idea to download the files/images.So i am leaning towards the below approach(you can either use NSOperationQueue ). 在我自己的问题上给出的答案理解GCD块[NSData dataWithContentsOfURL:URL]的行为确实有意义。所以如果你在GCD中使用[NSData dataWithContentsOfURL:URL] (这些天开发人员做的很多) )下载文件/图像不是一个好主意。所以我倾向于以下方法(你可以使用NSOperationQueue )。

Load your images using [NSURLConnection sendAsynchronousRequest:queue:completionHandler: then use NSCache to prevent downloading the same image again and again. 使用[NSURLConnection sendAsynchronousRequest:queue:completionHandler:加载图像,然后使用NSCache防止一次又一次地下载相同的图像。

As suggested by many developers go for SDWebimage and it does include the above strategy to download the images files .You can load as many images you want and the same URL won't be downloaded several times as per the author of the code 正如许多开发人员所建议的那样,请使用SDWebimage ,它确实包含了下载图像文件的上述策略。您可以加载任意数量的图像,并且根据代码的作者不会多次下载相同的URL

EDIT : 编辑

Example on [NSURLConnection sendAsynchronousRequest:queue:completionHandler: 关于[NSURLConnection sendAsynchronousRequest:queue:completionHandler:

NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:myUrlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{

    if ([data length] > 0 && error == nil)
        //doSomething With The data

    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        //time out error

    else if (error != nil)
        //download error
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM