簡體   English   中英

使用子視圖rect裁剪UIImage的區域

[英]Crop Area of UIImage with subview rect

這是我的困境。 我構建了一個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];
 }];

我很感謝我提供的幫助,因為我已經為此工作了好幾天,並且精疲力盡。 謝謝!

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