0

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. enter image description here

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
skram
  • 5,314
  • 1
  • 22
  • 26

2 Answers2

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;
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
  • Can you apply your solution to my example code? I tried implementing it and don't get the desired results. Thanks! @khoren-asatryan – skram Mar 20 '16 at 23:10
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;
}
skram
  • 5,314
  • 1
  • 22
  • 26