iOS的屏幕截图

时间:2013-01-10 08:01:37

标签: ios objective-c

我使用此代码进行iOS屏幕截图:

CGImageRef screen = UIGetScreenImage();
UIImage *image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);  
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
CGRect screenshotFrame;  
CGImageRef screenshotRef = CGImageCreateWithImageInRect(screenshotRef, screenshotFrame);
UIImage * screenshot = [[(UIImage *)screenshotRef retain] autorelease];
CGImageRelease(screenshotRef);

但我的屏幕包含屏幕底部的工具栏。我不想在我捕获的屏幕上使用工具栏。我怎样才能裁剪工具栏?我该如何修改我的代码?

4 个答案:

答案 0 :(得分:2)

我认为你可以捕获self.view层,因为它是根视图

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    //Retina display
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
    UIGraphicsBeginImageContext(self.view.bounds.size);
}
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

玩得开心!

答案 1 :(得分:2)

类似的问题已经标记为已解决。这里是您的解决方案的链接

iOS Screenshot part of the screen

答案 2 :(得分:1)

试试这个:

UIGraphicsBeginImageContext(/*CGRECT*/);     
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect传递你想要截图的矩形。

希望它可以帮助你..

答案 3 :(得分:0)

设置

CGRect screenshotFrame = CGRectMake(0, 0, 320, 436);
// 480(screen height) - 44(toolbar height) = 436