Is there a performance loss to receiving NSData and transforming it into a UIImage versus getting a UIImage directly?

StackOverflow https://stackoverflow.com/questions/21893762

When I download an image I usually use dataWithContentsOfURL: asynchronously, but it always returns a UIImage.

Is all data that comes over the network initially a form of NSData? Or does downloading it as NSData and then converting it to a UIImage serve as a waste, when I should be trying to download it as a UIImage directly?

有帮助吗?

解决方案

Essentially, They They DO The Same Thing(Either way You get NSData), So there is NO performance loss. If you want to improve performance, try SDWebImage (see: https://github.com/rs/SDWebImage). This library caches images and downloads them asynchronously.

其他提示

Data bytes come over the network. Those bytes can be treated as bytes (if you use NSData as the wrapper around them) or as an image (if the wrapper is UIImage). The underlying data bytes are just the same, it's just how you tell the app to use those bytes which is different. And there is a difference in how much code you have to write.

So, use which ever option makes more sense to you and in your situation. In the long run the number of operations run to download the data bytes and return an image to you is broadly the same.

Is there "performance loss" using dataWithContentsOfURL: not there is not. What you lose by using dataWithContentsOfURL: is a lot of functionality such as:

  • Can't follow the download progression
  • Can't cancel the download
  • Can't manage the possible authentication process
  • You can't handle errors easily, which is really important especially in mobile development like on iPhone of course (because you often lose your network in real conditions, so it is very important to track such network error cases when developing for iOS)

dataWithContentsOfURL: is suppose to be used to load local files per the documentation. From the docs:

Important: Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

What you should use is (from docs):

Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSSession class.

This is how you would use it:

-(void) sendHTTPGet
{
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

    NSURL * url = [NSURL URLWithString:@"http://theURL"];

    NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
                                                         completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(error == nil)
        {
            //use your data
        }

    }];

    [dataTask resume];

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top