简体   繁体   中英

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. 在此处输入图片说明

The blue bounding box is a subview of self.view, and the red bounding box is a subview of the blue bounding box. Im using AVCaptureStillImageOutput to generate the image; 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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