从UIImagePickerController调整UIImage的大小

时间:2014-03-18 18:00:53

标签: 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];
}

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

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

我想知道

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

3 个答案:

答案 0 :(得分: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;
}

whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk

resizing-and-cropping-a-uiimage

答案 1 :(得分:5)

您可以通过查询信息词典中的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
}

答案 2 :(得分:1)

  1. [self dismissModalViewControllerAnimated:YES completion:nil]方法的某处调用- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info。这是任何模态视图的常见做法。

  2. 这个问题可能比你想象的要复杂得多。有许多方法可以在iOS中调整照片大小,您使用的代码实际上取决于您的需求。但是,您可以查看此博客文章,以了解照片大小调整。它非常全面,我建议您在编写任何代码之前阅读它。

    http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

    据说这是实现调整大小的一种非常简单的方法:

    The simplest way to resize an UIImage?