ios - 从 UIImagePickerController 调整 UIImage 的大小

标签 ios objective-c uiimage uiimagepickercontroller

我目前正在使用这里的代码拍照

- (void) cameraButtonSelected
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

我允许用户编辑照片,但是当我出于某种原因使用此委托(delegate)方法时,在用户按下“使用照片”后,UIImagePickerController 不会从 View 中移除

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

我想知道

  1. 如何在按下“使用照片”按钮后从 View 中删除 UIImagePickerController
  2. 如何调整刚刚拍摄的照片的大小,因为我需要将较小的变体发送到我的服务器

最佳答案

您可以通过在信息字典中查询 UIImagePickerControllerEditedImage 来获取图像。 要从 View 中删除 ImagePicker,只需关闭选择器即可。 这是调整大小的代码。 只需用您的图像实例调用它

您可以使用此功能将图像缩放到特定尺寸

- (UIImage *) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

所以你的最终代码应该是这样的

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {    
    [picker dismissViewControllerAnimated:YES completion:Nil]; 
    UIImage *image = info[UIImagePickerControllerEditedImage];
    image = [self scaleImage:image toSize:CGSizeMake(200,200)]; // or some other size
}

关于ios - 从 UIImagePickerController 调整 UIImage 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487406/

相关文章:

ios - 如何从iphone中的png文件中检索单个图像

ios - 如何判断用户是否位于特定位置?

ios - 如何使用 MDCAertController Material Design 禁用触摸外屏关闭 View - swift

php - 从 iOS 到 PHP 脚本的 JSON 数据

ios - 更改 UIActionSheet 样式在 iOS 7 中没有区别吗?

iphone - 2 个库中的 Objective-C 链接器错误

ios - Xcode @2x 图像后缀在 iOS 中不显示为 Retina

ios - 我可以 NSLog 一个 JSON 并查看它的真实格式吗?

ios - 收到推送通知时打开特定选项卡并在 uiwebview 中启动特定网址

ios - 如何使用 UIBezierPath 创建 UIImage