UIImage

UIImage即图片,创建方法主要有4种:

1、imageNamed (实例方法)

UIImage * image = [UIImage imageNamed:@"sample"];

加载流程如下:

a.系统回去检查系统缓存中是否存在该名字的图像,如果存在则直接返回。

  b. 如果系统缓存中不存在该名字的图像,则会先加载到缓存中,在返回该对象。

观察上面的操作我们发现系统会缓存我们使用imageNamed:方法加载的图像时候,系统会自动帮我们缓存。这种机制适合于那种频繁用到界面贴图累的加载,但如果我们需要短时间内频繁的加载一些一次性的图像的话,最好不要使用这种方法。

2、imageWithContentsOfFile  (类方法)

UIImage * imageA = [UIImage imageWithContentsOfFile:@"path"];

3、initWithContentsOfFile

UIImage * imageB = [[UIImage alloc]initWithContentsOfFile:@"path"];

这两个方法跟前一个方法一样都是完成从文件加载图像的功能。但是不会经过系统缓存,直接从文件系统中加载并返回。

顺便提一下,当收到内存警告的时候,系统可能会将UIImage内部的存储图像的内存释放,下一次需要绘制的时候会重新去加载。

4、imageWithCGImage

该方法使用一个CGImageRef创建UIImage,在创建时还可以指定方法倍数以及旋转方向。当scale设置为1的时候,新创建的图像将和原图像尺寸一摸一样,而orientaion则可以指定新的图像的绘制方向。

UIImage的常用属性:

1、imageOrientation 控制UIImage的旋转方向

typedef NS_ENUM(NSInteger, UIImageOrientation) {
    UIImageOrientationUp,            // default orientation    
    UIImageOrientationDown,          // 180 deg rotation       
    UIImageOrientationLeft,          // 90 deg CCW          (编程发现官方文档中,left和right图像标反了,此处更正过来)
    UIImageOrientationRight,         // 90 deg CW         
    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip   
    UIImageOrientationDownMirrored,  // horizontal flip                             
    UIImageOrientationLeftMirrored,  // vertical flip                              
    UIImageOrientationRightMirrored, // vertical flip                              
};
  默认的方向是UIImageOrientationUp,这8种方向对应的绘制方如上面所示。我们在日常使用中经常会碰到把iPhone相册中的照片导入到windows中,发现方向不对的问题就是与这个属性有关,因为导出照片的时候,写exif中的方向信息时候没有考虑该方向的原因。

2、size 图像的尺寸

3、scale 图像的比例

4、CGImage\CIImage

5、images

如果有动画的图片,保存了所有的图片

6、duration




UIImageiOS中用于表示图像的类,可以用它来加载、显示和处理图片。以下是使用UIImage的基本步骤: 1. 导入UIImage库 ```objective-c #import <UIKit/UIKit.h> ``` 2. 加载图片 ```objective-c UIImage *image = [UIImage imageNamed:@"imageName"]; ``` 3. 显示图片 ```objective-c UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.view addSubview:imageView]; ``` 4. 处理图片 UIImage提供了一些方法来处理图片,例如裁剪、缩放、旋转等。以下是一些常用的方法: 裁剪图片 ```objective-c CGRect rect = CGRectMake(x, y, width, height); CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect); UIImage *newImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); ``` 缩放图片 ```objective-c CGSize newSize = CGSizeMake(width, height); UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); ``` 旋转图片 ```objective-c CGFloat radians = angle * M_PI / 180; CGAffineTransform transform = CGAffineTransformMakeRotation(radians); CGContextRef context = CGBitmapContextCreate(NULL, image.size.width, image.size.height, CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage)); CGContextConcatCTM(context, transform); CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage); CGImageRef newImageRef = CGBitmapContextCreateImage(context); UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; CGContextRelease(context); CGImageRelease(newImageRef); ``` 以上是UIImage的基本使用方法,你可以根据自己的需要来进行更多的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值