UIImage 使用UIGraphicsBeginImageContext做一些简单操作

UIImage 绘制


UIGraphicsBeginImageContext

步骤:

  1. UIGraphicsBeginImageContext(CGSize size) 或者 UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
    • 准备绘图环境
  • CGContextRef __nullable UIGraphicsGetCurrentContext(void)
    • 获取绘图的CGContextRef
  • 开始绘图
  • UIImage* UIGraphicsGetImageFromCurrentImageContext(void);
    • 获取当前绘制的图形
  • void UIGraphicsEndImageContext(void);
    • 关闭绘图环境

简单应用:

等比缩放 将图片改为自定义大小

- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
	UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
	[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
	UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return scaledImage;
}

将UIView转为UIImage

-(UIImage*) captureView:(UIView *)theView {
	CGRect rect = theView.frame;
	UIGraphicsBeginImageContext(rect.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	[theView.layer renderInContext:context];
	UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return img;
}	

** renderInContext 只会绘制在左上角 **

** 若要将UIView绘制在指定地方可以平移context **
如:

CGContextTranslateCTM(context, 50, 50);
[theView.layer renderInContext:context];
CGContextTranslateCTM(context, -50, -50);

根据给定得图片,从其指定区域截取一张新得图片

-(UIImage *)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef =CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}	

合并两张图片

-(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;
}

截取当前屏幕

CGSize windowSize = behandView.bounds.size;
UIGraphicsBeginImageContextWithOptions(windowSize, YES, 2.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[behandView.window.layer renderInContext:context];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();	

截取scrollview

-(UIImage *)captureScrollView:(UIScrollView *)scrollView{

UIImage* image = nil;
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 2.0f);

scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}
posted @ 2016-02-24 13:10  孙焱焱  阅读(3272)  评论(0编辑  收藏  举报