相关图像处理以及UIGraphicsBeginImageContext系列知识

本文详细介绍了iOS中UIGraphicsBeginImageContext的使用,包括如何进行等比缩放、自定义大小调整图片,以及如何存储图片到文件和照片库。此外,还展示了如何截取UIView和图片的特定区域,以及合并两张图片的方法。
摘要由CSDN通过智能技术生成

UIGraphicsBeginImageContext创建了一个基于位图的上下文,并将其设置为当前上下文(context).

Void  UIGraphicsBeginImageContext(CGSize  size);

Void  UIGraphicsBeginImageContextWithOptions(CGSizesize, BOOL opaque, CGFloat scale); //opaque透明开关,如果图形完全不用透明,设置YES优化位图存储;scale缩放因子


UIImage处理

1,        等比缩放

-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{

UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize, image.size.height * scaleSize); //scaleSize为缩放比例, 创建基于位图的图形上下文

[image drawInRect:CGRectMake(0, 0, image.size.width*scaleSize, image.size.height *scaleSize )];//绘图

UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();

//获得图片

UIGraphicsEndImageContext();

//从当前堆栈中删除quartz2D绘图环境

return scaledImage;  //返回缩放后的图片

}

2,        自定义大小

-(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)resize {

//创建基于位图的图形上下文

IGraphicsBeginImageContext(CGSizeMake(resize.width, resize.height));

[image drawInRect:CGRectMake(0, , 0, resize.width, reSize.height)];

UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();//从当前堆栈中删除quartz2D绘图环境

return reSizeImage

}

3,        存储图片

3.1存储到app文件中

NSString *path =[[NSHomeDirectory()stringByAppendingPathComponent:@”Documents”] stringByAppendingPathComponent:@”image.png];

[UIImagePNGRepresentation(image)  writeToFile:path  atomically:YES];

3.2存储到手机的图片库中

CGImageRef  screen =UIGetScreenImage();   //截取整个Screen

UIImage *image = [UIImage imageWithCGImage:screen];

CGImageRelease(screen);

UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);   //写到手机图片库

4,        对于特定UIView的截屏

-(UIImage *)captureView:(UIView *)theView

{
CGRect rect = theView.frame;

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

[theView.layer renderInContext:context];

UIImage *img= [UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return  img;

}

5,        截取指定区域

//方法一: 使用的200*200的图片,截取的是左上角

-(UIImage *)getImageFromImage{

//大图bigImage

//定义myImageRect,截图的区域

CGRect myImageRect = CGRectMake(0, 0, 100, 100);

UIImage* bigImage= [UIImage imageNamed:@"1.jpg"];

CGImageRef imageRef = bigImage.CGImage;

CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef,myImageRect);

CGSize size;

size.width = 100.0;

size.height = 100.0;

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, myImageRect, subImageRef);

UIImage* smallImage = [UIImageimageWithCGImage:subImageRef];

UIGraphicsEndImageContext();

return smallImage;

}

 

//方法二

UIImage *image=[UIImage imageNamed:@"1.jpg"];
CGRect rect = CGRectMake(0, 0, 100, 100);//创建矩形框
UIImageView *contentView = [[UIImageView alloc] initWithFrame:rect];
contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([imageCGImage], rect)];

6,        合并两张图片

 - (UIImage*)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {

UIGraphicsBeginImageContext(image1.size);

// Draw image1

[image1 drawInRect:CGRectMake(0, 0, image1.size.width,image1.size.height)];

// Draw image2

[image2 drawInRect:CGRectMake(0, 0, image2.size.width,image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值