【发布时间】:2015-07-07 01:28:08
【问题描述】:
我目前正在用我的
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 方法如下:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
picker.allowsEditing = YES;
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect cropRect = CGRectMake(0, 0, 2448, 3264);
UIImage *croppedImage = [img crop:cropRect];
imageToPass = croppedImage;
NSLog(@"Here's imageToPass: %@", imageToPass);
NSLog(@"and here's imageToPass' width: %f", imageToPass.size.width);
NSLog(@"and here's imageToPass' height: %f", imageToPass.size.height);
NSLog(@"and here's imageToPass' scale: %f", imageToPass.scale);
UINavigationController *postControl = [self.storyboard instantiateViewControllerWithIdentifier:@"postControl"];
RGPostViewController *postView = (RGPostViewController *)postControl.topViewController;
[postView storeImage:imageToPass];
[self.presentedViewController presentViewController:postControl animated:NO completion:nil];
}
我的问题是,当我打印 imageToPass 变量的宽度和高度时,我发现该值以点为单位列出。我的控制台打印如下:
我需要返回一个裁剪为 320x320 大小的图像。使用我的代码CGRect cropRect = CGRectMake(0, 0, 2448, 3264); 我采用照片的原始尺寸,默认情况下 UIImagePickerController 是,我假设,320x520 或类似的东西。使用点值我可以看到 2448 是点宽,3264 是点高度。来自谷歌,
iPhone 5 的显示分辨率为 1136 x 640 像素。新触摸屏的对角线尺寸为 4 英寸,纵横比为 16:9,并带有 326 ppi(每英寸像素)的 Retina 显示屏。
我不知道在这里做什么。 2448points/640px = 3.825 的数学是否告诉我在 326ppi 屏幕上每个像素有 3.825 个点?
PS 请记住,我正在尝试在 UIImagePickerControllerOriginalImage 中间抓取 320x320 图片,这意味着切割一些顶部像素数和一些底部像素数,以我假设的点数确定。
编辑
这是上面第四行代码中crop:方法的代码:
#import "UIImage+Crop.h"
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)rect {
rect = CGRectMake(rect.origin.x*self.scale,
rect.origin.y*self.scale,
rect.size.width*self.scale,
rect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
@end
我发现如果我将CGRect cropRect 设置为CGRect cropRect = CGRectMake(264, 0, 2448, 3000); //3264,它实际上会从图像的顶部和底部移除264 个点。我了解 iPhone 5s 的屏幕分辨率为 326ppi(每英寸像素),我该如何使用它来成功移除需要移除的像素数量。
【问题讨论】:
-
这应该比现在简单很多
-
你的意思是你想要一个 320x320 的正方形点从你的图像中心,还是你的意思是你想裁剪你的图像,使它成为正方形?
-
我想将我的图像裁剪成完美的正方形。我什至不确定图像为 320x320 意味着什么。因为它说屏幕是 640x1136,将它们除以 2 得到 320x568,这实际上是 Storyboard 文件中的视图。完全对所有这些测量感到困惑,但我只需要切断 UIImagePickerController 的顶部和底部即可“调整”图像。
-
在这种情况下,我会编辑您的问题并删除所有对 320x320 的引用,因为人们正试图使用该信息来回答。而且ppi也是一条红鲱鱼!我只想说“我如何裁剪 UIImage 以使其成为正方形”,然后看看你会得到什么答案。
标签: ios uiimage uiimagepickercontroller crop