Xcodeで画面上の指定された地域のスクリーンショットをキャプチャする方法は?
たとえば、画面上の (0,100,320,200) の領域をキャプチャして UIImage オブジェクトに保存したいと考えていますか?
ありがとう
Xcodeで画面上の指定された地域のスクリーンショットをキャプチャする方法は?
たとえば、画面上の (0,100,320,200) の領域をキャプチャして UIImage オブジェクトに保存したいと考えていますか?
ありがとう
フルスクリーンのスクリーンショット (キー ウィンドウ) については、以下のコードを試してください。
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
このコードを試して、ネイティブ解像度で UIView をキャプチャします
CGRect rect = [captureView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[captureView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
必要に応じて、UIImage を 95% の品質で jpg 形式でアプリのドキュメント フォルダーに保存します。
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];