方法一:
- (UIImage*)capture
{
UIGraphicsBeginImageContextWithOptions(self.frame.size,NO, [UIScreenmainScreen].scale);
[selfdrawViewHierarchyInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)afterScreenUpdates:YES];
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
方法二:
- (UIImage*)getImage {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width,self.frame.size.height),NO,1.0);//NO,YES控制是否透明
[self.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//生成后的image
return[selfgetImageFromImage:image];
}
//根据给定得图片,从其指定区域截取一张新得图片
-(UIImage*)getImageFromImage:(UIImage*)bigImage
{
//大图bigImage
//定义myImageRect,截图的区域
CGRectmyImageRect =CGRectMake(floatIntervalW+1,floatY+1,floatW-2,floatW-2);
CGImageRefimageRef = bigImage.CGImage;
CGImageRefsubImageRef =CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSizesize;
size.width=floatW-2;
size.height=floatW-2;
UIGraphicsBeginImageContext(size);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImageimageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
returnsmallImage;
}