IOS - UIImagePickerController - 约束图像的比例

时间:2014-01-16 14:09:57

标签: ios objective-c uiimagepickercontroller

我已经设置了一个简单的照片捕捉系统来捕捉IOS应用程序中的个人资料照片,除了我不明白如何约束图像的比例以适应120px x 120px配置文件图像帧之外,一切正常。

有人可以提出任何建议吗?

UIview如下所示 - enter image description here

我的代码如下 -

- (IBAction)takePhoto:(id)sender {
     if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera])
    {
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = NO;
    [self presentViewController:imagePicker
                       animated:YES completion:nil];
    _newMedia = YES;
   }


}

- (IBAction)editPicBtn:(id)sender {
    [_cameraPanel setHidden:NO];


 }

 -(void)imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

     [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    _photoSquare.image = image;

    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,

                                               @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
[_cameraPanel setHidden:YES];

}

-(void)image:(UIImage *)image
 finishedSavingWithError:(NSError *)error
 contextInfo:(void *)contextInfo
{
    if (error) {
      UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"Save failed"
                          message: @"Failed to save image"
                          delegate: nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
      [alert show];
 }
}

 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
 {
     [self dismissViewControllerAnimated:YES completion:nil];
 }

- (IBAction)useCameraRoll:(id)sender {
   if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeSavedPhotosAlbum])
   {
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = NO;
    [self presentViewController:imagePicker
                       animated:YES completion:nil];
    _newMedia = NO;
    }



}

1 个答案:

答案 0 :(得分:2)

从相机胶卷中选择图像时,可以选择缩放/定位图片,以便用户可以对现有照片进行方形裁剪。然后,您可以将方形图片的大小调整为所需的120x120像素图像。我不确定这个选项是否适用于新拍摄的照片,但我猜是这样。

如果Apple的标准裁剪工具不够用,您可以随时实施。这就是我所做的。

// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
imagePicker.allowsEditing = YES;

裁剪器返回640x640像素的方形图像,因此要将图像大小调整为120x120像素,您可以执行以下操作:

- (UIImage *)resizeImage:(UIImage *)image
{
    CGRect rect = CGRectMake(0.0, 0.0, 240.0, 240.0); // 240.0 rather then 120.0 for retina
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return img;
}