0

私はself.viewを持っていますが、300x320だけをキャプチャしたいと思います。

このコードを取得しました:

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

UIImage * imgPrintScreen = [[UIImage alloc]init];
imgPrintScreen = viewImage;

そのためには、ここで何を変更する必要がありますか?

割り当ててくれてありがとう!

4

1 に答える 1

2

境界全体を使用するのではなく、キャプチャするサイズを変更するだけで、必要なサイズを使用できます。レンダリングはビューの原点から開始され、境界の外に出るとクリップされます。ビューが実際にクリップされる場所を変更する必要がある場合は、画像コンテキストを開始した後にコンテキストを変換するだけです。

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

たとえば、ビューが600x320で、幅の中央の300ポイントをキャプチャする場合は、コンテキストを150ポイント左に変換します。

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, -150.f, 0.f);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2012-10-02T15:06:20.023 に答える