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

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 に答える