iOS下的图片加载

适用于自定义imageview的多图片加载

直接上代码:

#pragma mark---设置iamgeView的背景图片 占位图片
+(void)setImageViewBgimageWithURL:(NSString *)url zhanweiImage:(UIImage *)image imageView:(UIImageView *)imageView{
    if (!image) {
        
    }
    [imageView setImage:image];
    dispatch_queue_t queue=dispatch_queue_create("downimage", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        UIImage *image=[self setimageWithURL:url];
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image=image;
        });
    });
}

#pragma mark---下载图片 保存沙盒
+(UIImage *)setimageWithURL:(NSString *)url{
    NSData *data=[self images][url];
    if (data) {
        return [UIImage imageWithData:data];
    }
    NSString *cachesPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filename=[cachesPath stringByAppendingPathComponent:[url lastPathComponent]];
    data=[NSData dataWithContentsOfFile:filename ];
    if (!data) {
        data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        if (data) {
            [data writeToFile:filename atomically:YES];
            [self images][url]=data;
        }
    }
    return [UIImage imageWithData:data];
}

+(NSMutableDictionary *)images{
    static NSMutableDictionary *queue=nil;
    if (queue==nil) {
        queue=[NSMutableDictionary dictionaryWithCapacity:10];
    }
    return queue;
}

传入图片数组直接调用就好了:

for (NSInteger i = 0; i < images.count; i++) {
        UIImageView *imageview = (UIImageView *)[self viewWithTag:i+1];
        
        [CellImageViews setImageViewBgimageWithURL:images[i] zhanweiImage:nil imageView:imageview];
        
    }

 

转载于:https://my.oschina.net/u/2555813/blog/730733

你可能感兴趣的:(iOS下的图片加载)