0

我正在制作与图像相关的应用程序。我的屏幕上有多个图像。我已经截屏了。但是我想截取一些有限区域的屏幕截图,所以基本上我想限制屏幕截图的帧数。下面是我的屏幕截图代码。

-(UIImage *) screenshot
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES];

    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

截屏后,我在facebook分享方法中通过下面的代码使用它,

UIImage *image12 = [self screenshot];

[mySLComposerSheet addImage:image12];
4

2 回答 2

0

就像是:

-(UIImage *)ConvertToImage:(UIView *)view
{
    UIGraphicsBeginImageContext(view.frame.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return viewImage;
}
于 2013-11-14T11:25:26.500 回答
0

If I'm understanding correctly you want to take a screenshot and crop it. There are a lot of alternatives, this is one of them:

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); /* this is the full capture */
CGRect r = (CGRect){{image.size.width * 0.25, image.size.height * 0.25}, {image.size.width / 2, image.size.height / 2}};
CGImageRef ref = CGImageCreateWithImageInRect(image.CGImage, r);
UIImage *cropped = [UIImage imageWithCGImage:ref]; /* this is the center area */
CGImageRelease(ref);
UIGraphicsEndImageContext();

Just capture the right view and calculate the right rectangle.

In iOS 7 you can use the UIView's faster drawViewHierarchyInRect:afterScreenUpdates: instead of renderInContext.

于 2013-11-14T11:41:57.880 回答