http://blog.csdn.net/braver_smile/article/details/40893279
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 = CGImageCreateWithImageIn
CGSize size;
size.width = 100.0;
size.height = 100.0;
UIGraphicsBeginImageCont
CGContextRef context = UIGraphicsGetCurrentCont
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImageimageWithCGImage:subImageRef];
UIGraphicsEndImageContex
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 {
UIGraphicsBeginImageCont
// 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 = UIGraphicsGetImageFromCu
UIGraphicsEndImageContex
return resultingImage;
}