문제

I am making an app..

I need to download many pictures from a server, but I don't know how to do :(

I try to use this code:

UIImageView *image=(UIImageView *)[cell viewWithTag:100];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"FileName"]];
        if ( data == nil )
            return;
        dispatch_async(dispatch_get_main_queue(), ^{

            image.image=[UIImage imageWithData:data];

        });
        data=nil;
    });

This code is slow... I need to do it more quickly... I think AFNetworking is the best option, isn't it ?

도움이 되었습니까?

해결책

You have several options, one of the AFNetworking. However, if you go with AFNetworking, you should use the UIImageView+AFNetworking class, inside the UIKit+AFNetworking folder.

Here's the documentation for it: http://cocoadocs.org/docsets/AFNetworking/2.0.1/Categories/UIImageView+AFNetworking.html

Another great option is SDWebImage which gives you more advanced control over caching and image handling, such as processing images before they're displayed, handling your own caching, etc.

다른 팁

AFNetworking would be perfect for your needs. It has its own UIImageView category that handles asynchronous image loading very smoothly. Give it a try.

Like previous answers have suggested, AFNetworking is a great way to go.

I too, though, want to suggest using SDWebImage. There are a lot of nice features in there, such as cool cache handling and image decompression.

For starters, try doing something like

[self.image setImageWithURL:[NSURL URLWithString:imagePath]
               placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                   [image setImage:image];
}];

When it comes to speeding up the code, I am not sure why your code is slow in the first place since you are already doing it asynchronously.

AFNetworking is a fantastic framework and will make easy for you to do async image request.

However using AFNetworking will not speed up your code.

Probably will make your code slow, but the difference should be irrelevant, only if your images are too larger and the time to decode the image are high (I don't think this is the problem).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top