简体   繁体   中英

How can I calculate the crop size (rect) of the UIImage with different resolution

In my app, I have a 'Capture from Camera' function, I am using GPUImageFramework by Brad Larson (ty) , GPUImageStillCamera when capturing still images.

stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPresetPhoto cameraPosition:
               AVCaptureDevicePositionBack];

as you can see. the Session preset is AVCaptureSessionPresetPhoto which in iPhone 4 gives me 1529x2048 output and with 4s (8mp) device gives me 2448x3264.

the image below is how I use the GPUStillCamera. here, there is rectangle which is in this case, the crop rect of the captured image.

还是相机输出

code snippet when I capture image.

- (IBAction)takePhoto:(id)sender
{
        [stillCamera capturePhotoAsJPEGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedJPEG, NSError *error){

        //1529x2048 when iPhone 4 and 2448x3265 when on 4s
        UIImage *rawImage = [[UIImage alloc]initWithCGImage:[[UIImage imageWithData:processedJPEG]CGImage]scale:1.0 orientation:UIImageOrientationUp];

       **CGRect rect = CGRect{?,?,?,?};**
       UIImage *imageFromRect = [rawImage imageAtRect:rect ];
}

My question here is. How can I know the points (x,y,w,h) that I can pass to imageAtRect method that will draw subimage from the rawImage ? and will get only the image portion within the rectangle ( regardless the resolution of the captured image ) as shown in the screen above? should I just do it manually or is there any math technique you can suggest?

Thanks!

You can use this image cropping class and libray file and you can get the size and with and origin position of the cropped UIImage .

http://code4app.net/ios/Custom-image-cropper/51e5f03f6803fac530000000

Plus if you want it can dynamically can change the size of the cropping image accordingly. Easy to use and implement. Please look into the tutorial once.

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