从那时起,我就一直在尝试这样做。我有个摄像头。我想让我的最终图像成为可从内置摄像头查看的图像的一部分。我所做的是使CGRect的尺寸等于相机中的正方形。然后我试着用这个函数来裁剪它。
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}我这样叫它
CGRect rect = CGRectMake(10, 72, 300, 300);
UIImage *realImage = [self imageByCropping:[self.capturedImages objectAtIndex:0] toRect:rect];我得到的是一个方向错误的质量很差的图像。
::编辑::
根据Nitin的回答,我可以裁剪屏幕的正确部分,但问题是它裁剪了相机视图之后的视图,即“确认视图”。我怀疑这是因为Nitin的代码使用了
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
也因为所有这些都发生在ViewController中,因为确认视图的视图控制器是执行此代码的控制器。我将尝试用一张小地图来解释这一点。
CameraOverlay.xib(它使用此xib创建覆盖) <===== CameraOverlayViewController -> ConfirmationView
因此,当第一次调用ViewController (选项卡栏上的按钮)时,它会打开相机(UIImagePickerController),并在其上叠加。然后,一旦用户单击某个图像,该图像就会显示在ConfirmationView上。
我认为正在发生的事情是,当UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 1.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();这些行被执行时,当时的视图是ConfirmationView。
注意:我在
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法。
发布于 2013-08-19 18:22:08
请参阅Drawing and printing Guide。
CoreGraphics和UIKit之间的默认坐标系不同。我认为你的问题就是因为这个事实。
使用这些可能会帮助你解决这个问题
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context , 0.0, rect.size.height);
CGContextScaleCTM(context , 1.0, -1.0);https://stackoverflow.com/questions/18311191
复制相似问题