2017-02-17 55 views
-3

我正在iOS应用程序中使用相机从捕获的图像中选择卡矩形。所以如果有人知道任何解决方案,请让我知道。谢谢。从捕获的图像中选择卡矩形iOS

+0

缴获image'你的'选择卡长方形意味着切出一个形象? – aircraft

+0

是的,我想剪切图像,生成的图像应该是卡矩形区域。 –

回答

-1

我的代码如下:

/** 
* Cut out a image to a new image with the rect 
* 
* @param image UIImage image origin image 
* @param rect CGRect rect the rect area you want 
* 
* @return UIImage 
*/ 
+ (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect{ 

    CGFloat scale = [UIScreen mainScreen].scale; 
    CGFloat x= rect.origin.x*scale,y=rect.origin.y *scale,w=rect.size.width*scale,h=rect.size.height*scale; 
    CGRect dianRect = CGRectMake(x, y, w, h); 

    //cut the image with the rect area 
    CGImageRef sourceImageRef = [image CGImage]; 
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; 
    return newImage; 
} 
+0

谢谢@飞机的回复。我只有一个查询,那里的rect参数值是 –

+0

@PradumnaPatil,我的方法应该放在'Util'文件中,并且在捕获照片之后,调用方法,放入照片图像和所需的矩形,你可以得到你想要的结果。 – aircraft

+0

是的,我已经把你的方法放在Util类中,但是我应该不能理解我应该把什么放在矩形参数中,请你解释一下。谢谢 –