如何在ios中使用UIImageView + AFNetworking异步加载图像

时间:2015-12-02 10:47:41

标签: ios objective-c uiimage afnetworking

我正在使用它:

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://oldmaker.com/glamberry_v2/ios_Loading-Spinners.gif"]]];

但这需要花费太多时间。我想使用AFNetworking来加快加载速度。

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:8)

您可以使用SDWebImage

它支持异步下载和缓存。

用法

只需#import UIImageView+WebCache.h标题

  [imgView sd_setImageWithURL:Url_Of_The_Image placeholderImage:[UIImage imageNamed:@"Sampleimage.png"]];

答案 1 :(得分:4)

#import "UIImageView+AFNetworking.h"  

//...
[self.imageView setImageWithURL:[NSURL URLWithString:@"image_url"]];

答案 2 :(得分:4)

您可以像这样加载异步图像:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithData:imageData];
        });
    });

我需要将此图像设置为imageview,因此请尝试使用:

UIImageView *img = [[UIImageView alloc] initWithFrame:FRAME];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithData:imageData];
            img.image = image;
            img.contentMode = UIViewContentModeScaleAspectFill;
        });
    });

答案 3 :(得分:1)

似乎没有答案完全处理这个问题,所以,要获得更多信息...

首先,您应该包含UIImageView的AFNetworking类别:

#import "UIImageView+AFNetworking.h"

如果你正在使用CocoaPods,这一行应该是:

#import <AFNetworking/UIImageView+AFNetworking.h>

之后,您应该至少有一个UIImageView(我假设您已连接IBOutlet):

@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

然后您可以在代码中加载图片:

// Define a placeholder image that will be shown while the remote image
// loads. If you don't want a placeholder image, just set it to nil.
// Note that this placeholder image is local (it should be in your
// asset files/collection).
UIImage *myPlaceholderImage = nil; // Or use [UIImage imageNamed:@"..."];

// First, cancel other tasks that could be downloading images.
// If you only do this once, this is probably not needed, but it's not harmful to
// always have it before loading an image with setImageWithURL: and derivatives.
[self.myImageView cancelImageDownloadTask];

// Set up a NSURL for the image you want.
// I'm gonna use this wonderful owl: https://www.smashingmagazine.com/wp-content/uploads/2015/06/10-dithering-opt.jpg
NSURL *imageURL = [NSURL URLWithString:@"https://www.smashingmagazine.com/wp-content/uploads/2015/06/10-dithering-opt.jpg"];

// Check if the URL is valid
if ( imageURL ) {
    // The URL is valid so we'll use it to load the image asynchronously.
    // Pass the placeholder image that will be shown while the
    // remote image loads.
    [self.myImageView setImageWithURL:imageURL
                     placeholderImage:myPlaceholderImage];
}
else {
    // The imageURL is invalid, just show the placeholder image.
    dispatch_async(dispatch_get_main_queue(), ^{
        self.myImageView.image = myPlaceholderImage;
    });
}

如果出于任何原因,您需要在将远程图像传送到UIImageView之前对远程图像执行 ,则可以使用setImageWithURLRequest:placeholderImage:success:failure:方法。我将在模板模式下将收到的UIImage转换为另一个UIImage,因此它将使用tintColor的{​​{1}}进行着色(当然,这是猫头鹰的形象看起来不太好,这应该是一个透明的简单图标:

UIImageView

是的,我知道这是一个老问题,但这可以作为参考。

答案 4 :(得分:0)

Haneke也可以胜任。

[imageView hnk_setImageFromURL:url];

如果您想加载AFNetworking

[self.someImageView setImageWithURL:[NSURL URLWithString:@"urlOfImage"]];