ScreenShot of an UILabel and what is in a UIImageView

I am trying to take a screenshot of my UIImageView and a UILabel that is on top of that.

What I have so far grabs the UIImage in the ImageView and then renders the overlay on it but the positioning of the UILabel is all wrong. I am setting the size of the capture to the actual image size(which isn't what i want).

I just want to be able to take a screenshot exactly how it appears on the screen.

CGSize imageSize = self.imageFromOtherView.size;
// define the size and grab a UIImage from it
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

[self.imageFromOtherView drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];

[self.socialLabel.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Create a UIView to hold both the UIImageView and UILabel. Then pass this container view through this code. I have my view as a property called viewForPhoto so when the code is called it only captures that view. You can tweak it so that it receives a view. This will return the UIImage that you want.

- (UIImage *)imageByRenderingView
{
    UIGraphicsBeginImageContextWithOptions(self.viewForPhotoView.bounds.size, NO, 0.0);
    [self.viewForPhotoView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

将这些视图添加到公共容器视图中,然后调用- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates视图在上下文中呈现它。

链接地址: http://www.djcxy.com/p/68152.html

上一篇: 截取文件\文件夹I \ O没有API挂钩或过滤器驱动程序

下一篇: UILabel的屏幕快照和UIImageView中的内容