这是我的困境。我建立了一个QR扫描仪,需要将QR码左上角的彩色部分裁剪成自己的图像,以便进一步处理。
蓝色边界框是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];
}];
我非常感谢您对此的任何帮助,因为我已经好几天了,而且我的智慧结束了。谢谢!
答案 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;
}