UIImageView是一个可以显示图片的视图控件,其能够显示图片的本质也是通过绘图来实现的,即,首先加载一张图片(UIImage对象),然后在drawRect:方法中把图片绘制到绘图上下文中,并显示出来。
UIImage类中创建UIImage对象的方法:
// imageNamed:类方法,从main bundle中读取图片文件。通常情况下图片放置在工程的Assets.xcassets文件夹中,只要提供文件名即可创建UIImage对象。
+(nullable UIImage *)imageNamed:(NSString *)name;
// imageWithContentOfFile:类方法,从工程文件中读取图片,传入一个图片路径参数,最终得到一个UIImage对象。
+(nullable UIImage *)imageWithData:(NSData *)data;
// initWithContentsOfFile:实例方法,与imageWithContentOfFile类似,需要传入图片的路径。
-(nullable instancetype)initWithContentsOfFile:(NSString *)path;
绘图方法
// drawAtPoint:以图片的左上角为锚点,显示在制定的Point位置,图片不做压缩处理,显示原始大小。
-(void)drawAtPoint:(CGPoint)point;
// drawInRect:把图片装在一个矩形区域内显示,图片有可能会放大/缩小或者拉伸/压缩。
-(void)drawInRect:(CGRect)rect;
示例
-(void)drawRect:(CGRect)rect {
UIImage *image1 = [UIImage imageNamed:@"logo"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"ios" ofType:"png"];
UIImage *image2 = [UIImage imageWithContentsOfFile:path];
//
[image1 drawInRect:CGRectMake(0, 150, 100, 100)];
[image2 drawAtPoint:CGPointZero];
}
添加水印
对UIImage对象的修改和加工,经常会使用到UIGraphics类中提供的函数。对UIImage对象进行修改,通常会采取如下步骤:
1.调用UIGraphicsBeginImageContext方法,开启一个空白的图像上下文。
UIKITEXETERN void UIGraphicsBeginImageContext(CGSize size);
UIKITEXETERN void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
2.绘制该上下文中的内容,例如添加图片、添加文字等。
根据最新的图像上下文中的内容,调用UIGraphicsGetImageFromCurrentImageContext函数,得到一个图像合成后的UIImage对象。
UIKITEXETERN UIImage * __null_unspecified UIGraphicsGetImageFromCurrentImageContext(void);
3.调用UIGraphicsEndImageContext函数,关闭上下文,以节省内存。
UIKITEXETERN void UIGraphicsEndImageContext(void);
示例
-(UIImage *)addWatermarkInImage:(UIImage *)image WithText:(NSString *)string {
// 开启一个图形上下文
UIGraphicsBeginImageContext(image.size);
// 绘制上下文 - 绘制图片
[image drawAtPoint:CGPointMake(0, 20)];
// -添加文字到上下文
NSDictionary *dict = @{
NSFontAttributeName:[UIFont systemFontOdSize:12.0],
NSForegroundColorAttributeName:[UIColor blackColor]
};
[string drawAtPoint:CGPointMake(0, 20) withAttributes:dict];
// 从图形上下文中获取合成的图片
UIImage *watermarkImage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return watermarkImage;
}
在控制器类中,可以添加一张图片,通过调用addWatermarkInImage:WithText:方法,把添加水印后的合成图片显示出来
-(void)viewDidLoad {
// ...
// 添加水印
imageView.image = [self addWatermarkInImage:image WithText:string];
[self.view addSubview:imageView];
}
裁剪圆形图片
-(UIImage *)clipImage:(UIImage *)image {
//
UIGraphicsBrginImageContextWithOptions(image.size, NO, 0.0);
//
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRext:CGRectMake(0, 0, image.size.width, image.size.height)];
// 裁剪图片
[path addClip]
//
[image drawInRecr:CGRectMake(0, 0, image.size.width, image.size.height)];
//
UIImage *newImage = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();
return newImage;
}
在viewDidLoad方法中,为self.clipImageView对象设置image属性。
-(void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"iosimage"];
image = [self clipImage:image];
[self.clipImageView.image=image];
}
截屏
通常情况下,在应用中添加截屏功能需要有如下两个步骤。
1.获取当前屏幕截屏图片。在UIView类中,提供了drawViewHierarchyInRect:afterScreenUpdates方法,
利用该方法可以实现对任何View视图的截取图像操作。在使用之前,需要提前准备绘图上下文。
-(BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates;
2.保存图片到本地相册。在UIImagePickerController类中,封装了一个专门用于保存图片到相册的函数
UIImageWriteToSavedPhotosAlbum。通过该函数可以把UIImage图片对象存放到手机相册中。
UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, __nullable id completionTarget, __nullable SEL completionSelector, void* __nullable contextInfo);
示例
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIGraphicsBeginImageContext(self.view.bounds.size);
// 截屏
if ([self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO]) {
NSLog(@" Success");
}
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 保存到相册
UIImageWriteToSavedPhotosAlbum(screenshot, self, nil, nil);
}