截取view指定区域
- (UIImage*)imageFromView:(UIView *)view rect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 1.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGRect myImageRect = rect;
CGImageRef imageRef = image.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef,myImageRect );
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
CGImageRelease(subImageRef);
UIGraphicsEndImageContext();
return smallImage;
}
截取web指定区域
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)frame
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *img;
if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
{
for(UIView *subview in theView.subviews)
{
[subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES];
}
img = UIGraphicsGetImageFromCurrentImageContext();
}
else
{
CGContextSaveGState(context);
[theView.layer renderInContext:context];
img = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, frame);
UIImage *CGImg = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
return CGImg;
}