从捕获的图像iOS中选择卡片矩形

时间:2017-02-17 05:47:38

标签: ios objective-c uiimage

我在iOS应用程序中工作,我想使用相机从捕获的图像中选择卡片矩形。如果有人知道任何解决方案,请告诉我。谢谢。

1 个答案:

答案 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;
}