从UIImagePickerController调整UIImage的大小

Hur*_*rkS 2 objective-c uiimagepickercontroller uiimage ios

我目前正在使用此代码拍照

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

    [self presentViewController:picker animated:YES completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)

我允许用户编辑照片,但是当我因某种原因使用此委托方法时,UIImagePickerController在用户按下"使用照片"后不会从视图中删除

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
Run Code Online (Sandbox Code Playgroud)

我想知道

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

任何帮助,将不胜感激.

Paw*_*Rai 5

很简单,你可以在stackoverflow上搜索它

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 UIImage *tempImage=[info objectForKey:UIImagePickerControllerEditedImage];

 [self.dealImageView setImage:[self imageWithImage:tempImage convertToSize:CGSizeMake(200, 200)]];

 [self dismissViewControllerAnimated:YES completion:nil];

}

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {

  UIGraphicsBeginImageContext(size);
  [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return destImage;
}
Run Code Online (Sandbox Code Playgroud)

什么最最容易的方式对调整大小,优化-AN-图像尺寸与最iphone-SDK

调整和熟-A-的UIImage


Shu*_*ank 5

您可以通过查询UIImagePickerControllerEditedImage信息字典来获取图像.并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;
}
Run Code Online (Sandbox Code Playgroud)

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

- (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
}
Run Code Online (Sandbox Code Playgroud)