iOS屏幕截图的一部分 [英] iOS Screenshot part of the screen
问题描述
我有一个应用程序,它使用以下代码获取UIImageView的屏幕截图:
I have an App that takes a screenshot of a UIImageView with the following code:
-(IBAction) screenShot: (id) sender{
UIGraphicsBeginImageContext(sshot.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil);
}
这很有效,但我需要成为能够定位我拍摄截图的位置基本上我需要只有三分之一的屏幕(中心部分)。我尝试使用
This works well but I need to be able to position where I take the screenshot basically I need to grad only a third of the screen (center portion). I tried using
UIGraphicsBeginImageContext(CGSize 150,150);
但是发现每件东西都取自0,0坐标,有没有人知道如何定位这是正确的。
But have found that every thing is taken from 0,0 coordinates, has anyone any idea how to position this correctly.
推荐答案
截图是从您绘制的画布中截取的。
因此,不是在整个上下文中绘制图层,而是在左上角引用,您将在想要截取屏幕截图的地方绘制它....
Well the screenshot is taken from a canvas you draw. So instead of drawing your layer in the whole context, with a reference to top left corner, you will draw it where you want to take the screenshot....
//first we will make an UIImage from your view
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(sshot.frame.size);
[sourceImage drawAtPoint:CGPointMake(-50, -100)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
这篇关于iOS屏幕截图的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!