具有子视图rect的UIImage裁剪区域

时间:2016-03-20 22:47:47

标签: ios objective-c uiimage

这是我的困境。我建立了一个QR扫描仪,需要将QR码左上角的彩色部分裁剪成自己的图像,以便进一步处理。 enter image description here

蓝色边界框是self.view的子视图,红色边界框是蓝色边界框的子视图。我使用AVCaptureStillImageOutput生成图像;代码如下..

    [_imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

     NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *takenImage = [UIImage imageWithData:jpegData];

     //crop and process takenImage
     // tried utilizing convertRect:toView: and crop result is completely wrong.
     CGRect redFrame = [_colorBox convertRect:_colorBox.bounds toView:self.view];
 }];

我非常感谢您对此的任何帮助,因为我已经好几天了,而且我的智慧结束了。谢谢!

2 个答案:

答案 0 :(得分:0)

CGSize screenS = supperFrame.size;

CGFloat delX = image.size.width / screenS.width;
CGFloat delY = image.size.height / screenS.height;

CGRect sourceRect = CGRectMake(frame.origin.x * delX, frame.origin.y * delY, frame.size.width * delX, frame.size.height * delY);
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, sourceRect);


UIImage *cropImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropImage;

答案 1 :(得分:0)

- (UIImage *)imageAtFrameImage:(UIImage *)image creopFrame:(CGRect)creopFrame supperFrame:(CGRect)supperFrame {

    CGSize screenS = supperFrame.size;

    CGFloat delX = image.size.width / screenS.width;
    CGFloat delY = image.size.height / screenS.height;

    CGRect sourceRect = CGRectMake(creopFrame.origin.x * delX, creopFrame.origin.y * delY, creopFrame.size.width * delX, creopFrame.size.height * delY);
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, sourceRect);


    UIImage *cropImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return cropImage;
}