简体   繁体   English

使用子视图rect裁剪UIImage的区域

[英]Crop Area of UIImage with subview rect

This is my dilemma. 这是我的困境。 I built a QR Scanner and need to crop out the top left colored portion of the QR Code into it's own image for further processing. 我构建了一个QR扫描仪,需要将QR码的左上角颜色部分裁剪为自己的图像,以进行进一步处理。 在此处输入图片说明

The blue bounding box is a subview of self.view, and the red bounding box is a subview of the blue bounding box. 蓝色边界框是self.view的子视图,红色边界框是蓝色边界框的子视图。 Im using AVCaptureStillImageOutput to generate the image; 我使用AVCaptureStillImageOutput生成图像; code below.. 下面的代码

    [_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];
 }];

Id really appreciate any assistance with this as I've been at it for days, and am at my wits end. 我很感谢我提供的帮助,因为我已经为此工作了好几天,并且精疲力尽。 Thanks! 谢谢!

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;
- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM