在Objective-C中截取屏幕截图而不会降低质量

时间:2014-06-05 01:38:27

标签: ios objective-c xcode screenshot

我想要实现的是在我的应用程序中截取屏幕截图工作正常,但质量非常差,屏幕截图的大小也非常小。我也尝试裁剪图像的底部,就像我在顶部做的那样,但是我看不到在哪里可以为底部添加尺寸。有一个更好的方法吗?我使用下面的代码:

// Define the dimensions of the screenshot you want to take (the entire screen in this case)
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //now we will position the image, X/Y away from top left corner to get the portion we want
    UIGraphicsBeginImageContext(self.view.frame.size);
    [sourceImage drawAtPoint:CGPointMake(0, -10)];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);

感谢您的回答。

2 个答案:

答案 0 :(得分:4)

替换

UIGraphicsBeginImageContext(self.view.bounds.size);

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);

答案 1 :(得分:0)

这是我用来将视图内容保存到桌面上的2个png文件(低分辨率和高分辨率)的代码。

// High resolution
UIGraphicsBeginImageContextWithOptions(drawView.bounds.size, NO, 0.0);
[[drawView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *ViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *pngData = UIImagePNGRepresentation(ViewImage);
[pngData writeToFile:@"/Users/_username_/Desktop/Image@2x.png" atomically:NO];

// Low resolution
UIGraphicsBeginImageContext(drawView.bounds.size);
[[drawView layer] renderInContext:UIGraphicsGetCurrentContext()];
ViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

pngData = UIImagePNGRepresentation(ViewImage);
[pngData writeToFile:@"/Users/_username_/Desktop/Image.png" atomically:NO];