简体   繁体   English

如何以不同的分辨率计算UIImage的裁剪尺寸(rect)

[英]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. 在我的应用程序中,我有一个'从相机捕获'功能,我正在使用Brad Larson(ty)的GPUImageFramework,GPUImageStillCamera捕获静态图像时。

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. 会话预设是AVCaptureSessionPresetPhoto ,在iPhone 4中给我1529x2048输出和4s(8mp)设备给我2448x3264。

the image below is how I use the GPUStillCamera. 下图是我如何使用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 ? 我怎么能知道点(X,Y,W,H),我可以传递给imageAtRect方法将从绘制子图像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 . 您可以使用此图像裁剪类和libray文件,您可以获取裁剪的UIImage的大小和原点位置。

http://code4app.net/ios/Custom-image-cropper/51e5f03f6803fac530000000 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. 请看一下教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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