加载图片的两个方法:

  • [UIImage imageNamed:]
  • [[UIImage alloc] initWithContentsOfFile: imgpath]

[UIImage imageNamed:] : 加载的图片会自动缓存到内存中,不适合加载大图,内存紧张的时候可能会移除图片,需要重新加载,那么在界面切换的时候可能会引起性能下降

[[UIImage alloc] initWithContentsOfFile: imgpath]:加载图片,但未对图片进行解压,可以提前进行解压,提升加载速度.

    //异步加载图片
CGSize imgViewS = imgView.bounds.size;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSInteger index = indexPath.row;
NSString*imgpath = _imagePaths[index]; //绘制到context 提前解压图片
UIImage *img = [[UIImage alloc] initWithContentsOfFile: imgpath];
UIGraphicsBeginImageContextWithOptions(imgViewS , false, );
//这里也可以对图片进行压缩
[img drawInRect:CGRectMake(, , imgViewS.width, imgViewS.height)];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//模拟延迟加载
[NSThread sleepForTimeInterval:]; dispatch_async(dispatch_get_main_queue(), ^{
//加载当前cell对应的图片
if (cell.tag == index) {
imgView.image = img;
NSLog(@"加载图片。。。。");
} });

二.由于每次加载都需要解压,每次解压都需要消耗内存,所以可以利用NSCahe缓存好加载过的图片

/**
利用NSCache缓存图片 */
- (UIImage*)loadImageIndex:(NSInteger)index {
static NSCache *cache = nil;
if (cache == nil) {
cache = [[NSCache alloc] init];
//最大缓存
// [cache setCountLimit:1024];
//每个最大缓存
// [cache setTotalCostLimit:1024];
}
UIImage *img = [cache objectForKey:@(index)];
if (img) {
return [img isKindOfClass:[NSNull class]]?nil:img;
}
//设置占位,防止图片多次加载
[cache setObject:[NSNull null] forKey:@(index)]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSString *imgpath = self.imagePaths[index];
UIImage *loadimg = [UIImage imageWithContentsOfFile:imgpath];
//渲染图片到contenx
UIGraphicsBeginImageContextWithOptions(loadimg.size, false, );
[loadimg drawAtPoint:CGPointZero]; loadimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
//缓存图片
[cache setObject:loadimg forKey:@(index)]; NSIndexPath *indexpath = [NSIndexPath indexPathForRow:index inSection:];
UICollectionViewCell *cell = [self.collectView cellForItemAtIndexPath:indexpath];
if (cell != nil) {
UIImageView*imgV = [cell.contentView viewWithTag:];
imgV.image = loadimg;
} });
});
//未加载
return nil;
}

关于UIImageView缓存加载的笔记的更多相关文章

  1. Android批量图片加载经典系列——afinal框架实现图片的异步缓存加载

    一.问题描述 在之前的系列文章中,我们使用了Volley和Xutil框架实现图片的缓存加载(查看系列文章:http://www.cnblogs.com/jerehedu/p/4607599.html# ...

  2. Expo大作战(十三)--expo如何自定义状态了statusBar以及expo中如何处理脱机缓存加载 offline support

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  3. UIImageView异步加载网络图片

    在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...

  4. ios UIImageView异步加载网络图片

    方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...

  5. unity3d 加密资源并缓存加载

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止 ...

  6. android 中使用缓存加载数据

    最近app快完工了,但是很多列表加载,新闻咨询等数据一直从网络请求,速度很慢,影响用户体验,所以寻思用缓存来加载一些更新要求不太高的数据 废话不多说,上代码 欢迎转载,但请保留文章原始出处:)  博客 ...

  7. Android 开发 图片网络缓存加载框架Fresco

    简介 Fresco是一个在Android应用程序中显示图像的强大系统. Fresco负责图像的加载和显示.它将从网络.本地存储或本地资源加载图像,图像加载完成前会显示一个占位图片.它有两个级别的缓存: ...

  8. u3d 加密资源并缓存加载

    // C# Example // Builds an asset bundle from the selected objects in the project view. // Once compi ...

  9. ios UIImageView异步加载网络图片2

    //1. NSData dataWithContentsOfURL // [self.icon setImage:[UIImage imageWithData:[NSData dataWithCont ...

随机推荐

  1. 滚动事件优化 passive

    1.addEventListener参数 target.addEventListener(type, listener[, options]); target.addEventListener(typ ...

  2. 010 @ControllerAdvice

    一:说明 1.说明 这个注解是用于写一个异常捕获的处理类. 这里介绍全局捕获异常,自定义异常捕获 2.ps 在这里,顺便写一下基础的自定义异常类,以后方便用于业务异常继承 二:全局异常捕获 1.处理类 ...

  3. nginx-rtmp

    最近使用ffmpeg推流为rtmp,进行直播.记录一下nginx-rtmp的配置.因为我们要添加ngin-rtmp模块,所以不能直接apt安装. 下载niginx和nginx-rtmp-module的 ...

  4. linux环境中,openssl升级及openresty中nginx基于新版本openssl重新编译

    需求说明: 最近在对系统进行安全扫描的时候,出现了openssl版本的问题,建议对openssl版本进行升级,在此记录下升级过程. 环境说明: 操作系统:RHEL 6.6 升级操作过程: 1.下载最新 ...

  5. bim模型中所有IfcWallStandardCase构件

    ifc中的IfcWallStandardCase构件 //执行吊装 void startHoisting() { osg::Vec3f vec3f1 = index_node1->getBoun ...

  6. Flutter中通过普通的点击事件修改TextFormField的值

    import 'package:flutter/material.dart'; import 'package:zhongfa_apps/widget/public/PublicWidget.dart ...

  7. 判断命令test

    判断命令test一般用于脚本当中,可以简写为中括号[ ].其会对跟随的条件进行判断,一般可以分为数值判断.字符串判断和文件判断.语法格式为test [判断条件]或[ 判断条件 ],注意中括号[ ]与判 ...

  8. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  9. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  10. k8s Pod 扩容和缩容

    在生产环境下,在面临服务需要扩容的场景时,可以使用Deployment/RC的Scale机制来实现.Kubernetes支持对Pod的手动扩容和自动扩容. 手动扩容缩容 通过执行扩容命令,对某个dep ...