ios的UIImage的两种不同的图片加载方式 tom猫

在ios的UI交互设计时,对图片的处理是难免的;不同的处理方式会对内存有不同的影响;

************************************************************

a:图片格式及NSBundle加载全路径:

  1》xcode或者说苹果官方是极力推荐使用的图片格式是png

  2》所有如果项目中用得是png的图片,则不用写后缀名

  3》其他格式要求后缀名,特别是用UIImage加载图片时

  NSBundle加载全路径的常用代码:

  

?





1

2

3

4

// 得到文件的路径

NSString
*fullPath = [[NSBundle
mainBundle] pathForResource:namePic ofType:nil];

// 从路径中读取图片

UIImage *newImg = [[UIImage alloc] initWithContentsOfFile:fullPath];

  

************************************************************

b:以tom猫为例,看待UIImage的两种不同形式的图片加载

  tom猫用得是逐帧序列动画:背景是一个满屏的UIImageView,然后再UIImageView中一张张的切换图片;

  1》_tomList是一个UIImageView类型的类的属性;那么播放逐帧序列动画的步骤

  


// 1: 设置要播放逐帧序列动画的图片
_tomList animationImages = array; // array通常是个图片数组
// 2: 设置动画的时长:
_tomList animationDuration = 2.0;
// 3:设置重复次数
_tomList animationRepeatCount = 1;
...
...
// 最后:开始动画
[_tomImage startAnimating];

  array里的图片加载的方式

  1:UIImage *img = [UIImage imageNamed:namePic];

  // UIImage
imageNamed这种方式是有缓存的,如果是第二次调用,它不是从文件中取,而是直接从缓存中拿,也就是说内存会越来越大

  但是直接从内存中取图片,速度肯定快一点,性能高一点

  2: NSString *fullPath =
[[NSBundle mainBundle] pathForResource:namePic ofType:nil];

UIImage *newImg = [[UIImage alloc] initWithContentsOfFile:fullPath];

    
 这样是不会有缓存的,是使用全路径加载;但是也不会再动画播放完成后自动释放;只有进行下一次动画才会被释放;所以一般我们可以手动释放   

//
动画播放完成后,释放图片

[_tomImage performSelector:@selector(animationImages)
withObject:nilafterDelay:self.tomImage.animationDuration];

总结:两种方式各有利弊,imageNamed性能高一点,但是前提是图片占用的内存较小,且该图被多个地方或频繁使用;

    但是如果,图片存储空间大,内存吃不消,就得用initWithContentsOfFile:fullPath。

时间: 2024-08-10 19:18:05

ios的UIImage的两种不同的图片加载方式 tom猫的相关文章

iOS 正确选择图片加载方式

正确选择图片加载方式能够对内存优化起到很大的作用,常见的图片加载方式有下面三种: //方法1 UIImage *imag1 = [UIImage imageNamed:@"image.png"]; //方法2 UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.png" ofType:nil]]; //方法3 NSData

正确选择图片加载方式能够对内存优化起到很大的作用,常见的图片加载方式有下面三种:

1 2 3 4 5 6 7 //方法1   UIImage *imag1 = [UIImage imageNamed:@"image.png"];   //方法2   UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.png" ofType:nil]];   //方法3   NSData *imageData = [NSD

imageNamed , imageWithContentsOfFile , initWithContentsFile 三种图片加载方式的区别

UIImage常用的加载图片有3种方式: imageNamed , imageWithContentsOfFile , initWithContentsFile .imageNamed:UIImage image = [UIImage imageNamed:@"image.gif"] . 得到的对象是autoRelease的.这个方法有点特殊,它在生成image对象的同时,会把图像数据 根据它的名字缓存在系统内存中,以提高imageNamed方法获得相同图片的image对象的性能.即使

jquery文档加载几种写法,图片加载写法

jquery文档加载写法: $(function(){ }) ; //个人最常使用方式 $(document).ready(function(){ }); //调用文档对象下的ready方法传入一个函数. $(window).load(function() { });//调用window对象下的load方法传入一个函数. (function() { })(jQuery)//()()表示立即执行 并且传入jquery = $ 所以之前$也可以替换为jQuery 注意: jquery的ready只是

UIImage的两种加载方式

UIImage的两种加载方式 1.有缓存:读取后放入缓存中下次可直接读取,适用于图片较少且频繁使用. [UIImage imageNamed:@"文件名"]: 在缓存中由系统管理,当收到memoryWarning时会释放这些内存资源. 2.无缓存:用完就释放掉,参数传的是全路径,适用于图片较多较大的情况下. NSString *path = [[NSBundlemainBundle] pathForResource: @"1.png"ofType: nil]; [U

iOS开发笔记-两种单例模式的写法

iOS开发笔记-两种单例模式的写法 单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" static ServiceManager *defaultManager; @implementation ServiceManager +(ServiceManager *)defaultManager{ if(!defaultManager) defaultManager=[[self allocWith

【原】ios打包ipa的两种实用方法(.app转.ipa)

总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选一,一般选后两个. 局限性:个人开发一般采用这种方法,但是当一个证书多人使用时就稍显麻烦.一般多人开发时都是采用provisioning profile+P12文件来进行真机调试.上述方法在最后导出ipa包时需要输入appleID,这时还要向团队的其他人要.采用provisioning profile+P12

关于设置iOS横竖屏的两种方式(转载)

iPhone的横屏竖屏针对iOS系统版本分为两种开发方式: 一种是iOS 6之前的使用模式 一种是iOS6的新模式. 两者的区别还是蛮大的. 1:iOS6之前通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法.如下示例,设置以后,屏幕被旋转时只支持横屏转换: - (BOO

两种 设置 背景图片方法

两种设置背景图片方法 //    UIView *navigationbarTitleTupian =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 30)]; //    UIImageView *imageTitleTupian =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg.png"]]; // //    [navigationbarTitleTupian