iOS以编程方式截取屏幕图像

时间:2013-09-07 09:37:51

标签: ios objective-c uiview core-graphics quartz-core

我希望每次用户按住屏幕一秒钟时拍摄一个屏幕截图,然后打开一个电子邮件窗口让用户发送图像。但我有一个奇怪的问题;如果存在渐变图像(UIImage,其中加载了PNG渐变,则图像会混乱 - 将解释)。

所以我创建了UILongPressGestureRecgnizer,将minimumPressDuration设置为1.0f并将其作为手势识别器添加到主视图中:[self.view addGestureRecognizer:myRecognizer]。识别器调用方法,让我们说shareClicked。我想在其中捕获当前屏幕并弹出包含该图像的电子邮件编辑器。我是这样做的:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.view.bounds.size);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Share feature"];
    [mailComposer setMessageBody:[NSString stringWithFormat:@"Im sharing this with you cause its cool."] isHTML:NO];
    [mailComposer addAttachmentData:UIImagePNGRepresentation(image) mimeType:@"image/png" fileName:@"image"];

    [self presentViewController:mailComposer animated:YES completion:NULL];

但是,有一个问题。主视图上有一个表视图,第三个单元格中有一个我前面提到的渐变。我不知道如何解释它,但它是这样的:

enter image description here

在图像上可见,顶部有一个渐变,另一个稍低一些。顶部的一个正常渲染,而第二个导致一个奇怪的问题。渐变实际上不是蓝色,而是白色并且变为透明。这是黑色表面上的渐变图像: enter image description here

以下是模拟器的屏幕截图:

enter image description here

最后,它是如何在邮件编辑器中呈现和显示的:

enter image description here

我做错了什么?问题是什么?它不是模拟器的故障,因为它在移动设备上也是如此。它的iOS7如果有所不同。

为了清楚起见,顶部渐变是相同的但是径向并且它完美呈现。两个渐变都是tableview的单元格的子视图。怎么解决?希望我们找到一个解决方案,因为我花了一些时间来“制作”这个问题:)

1 个答案:

答案 0 :(得分:1)

查看推荐的解决方案https://developer.apple.com/library/ios/qa/qa1703/_index.html#//apple_ref/doc/uid/DTS40010193,因为不再允许GetScreenImage