2012-03-14 6 views
2

私はすべてのビューとレイヤーをマージして画面の一部のスクリーンショットを保存しようとしています。私がこれを行う方法を知っている唯一の方法は、以下のコードです。アプリのウィンドウの一部のスクリーンショットを撮るには?

以下のコードは、80x80の正方形を作成しますが、左上隅を0,0にします。

フルページのスクリーンショットから小さな矩形を切り取るにはどうすればよいですか?たとえば、320x480のフルウィンドウのスクリーンショットがありますが、160,240を中心とする80x80の正方形しか必要ありません。

UIGraphicsBeginImageContext(smallView.frame.size); 
[appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 

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

ありがとうございます!

+0

を使用して必要な部分をトリミング? – tams

答えて

9

はおそらく、あなたがイメージマスクを追加し、マスクされていない領域からコンテキストを取得することができ、この

CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect); 
UIImage *result = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 
3

未テストの提案...スクリーンショットの画像を取得した後

UIGraphicsBeginImageContext(smallView.frame.size); 
    [appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    CGRect cropRect = UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    CGRectMake(160.0,240.0,smallView.frame.size.width,smallView.frame.size.height); 
    CGImageRef croppedImageRef = CGImageCreateWithImageInRect(screenshot.CGImage,cropRect); 
    UIImage *croppedScreenshot = [UIImage imageWithCGImage:croppedImageRef]; 
    CGImageRelease(croppedImageRef); 
    UIGraphicsEndImageContext(); 
関連する問題