1

我使用此代码进行 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

3 回答 3

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();

玩得开心!

于 2013-01-10T08:17:34.307 回答
1

尝试这个:

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

在这里CGRect传递您要截屏的矩形。

希望对你有帮助。。

于 2013-01-10T08:04:28.787 回答
0

设置您的

CGRect screenshotFrame = CGRectMake(0, 0, 320, 436);
// 480(screen height) - 44(toolbar height) = 436
于 2013-01-10T08:08:27.110 回答