使用'UIImagePickerController'和'allowEditing'处理以获得完美的正方形

时间:2013-08-25 12:45:46

标签: iphone ios uiimagepickercontroller

如何确保UIImagePickerController始终返回平方图像? WhatsApp做到了,但我还没有找到实现这个目标的方法。

它在我的应用程序中的表现(在iOS 7中构建)

enter image description here

它应该如何(WhatsApp - iOS 6)

enter image description here

我的代码:

- (IBAction)showImagePicker:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;

    if ([sender tag] == 1) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }

    [self presentModalViewController:imagePicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    _imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self dismissModalViewControllerAnimated:YES];
}

1 个答案:

答案 0 :(得分:0)

要更改所捕获图像的大小,您需要在IB中的UIImageView中设置视图模式以缩放以填充。然后当从图书馆或相机中选择图像时,它会自动调整大小。你必须关闭自动布局,因为它不会允许ImageView调整大小。你的代码很好。无论如何这里是你可以构建的示例应用程序代码,以便更好地理解它。

·H

#import <UIKit/UIKit.h>

@interface ImageTestViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)takePhoto:  (UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;

@end

的.m

#import "ImageTestController.h"

@interface ImageTestViewController ()

@end

@implementation ImageTestViewController

- (void)viewDidLoad {

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Device has no camera"
                                                        delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles: nil];
        [myAlertView show];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (IBAction)takePhoto:(UIButton *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

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

- (IBAction)selectPhoto:(UIButton *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

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

@end

在故事板中的ImageTestController Xib或视图中,放置两个按钮,每个动作一个按钮,并在IB中创建必要的链接。然后放置UIImageView以填充视图中所需的任何大小。单击属性检查器的UIImageView和视图部分,将模式更改为要缩放以填充。关闭autolayout。尝试将图像添加到模拟器中以进行测试。确保图像具有横向大小。构建并运行,当您从图像库中选择图像时,它会自动调整为方形或您设置UIImageView的任何其他尺寸。

我知道你知道上面的大多数步骤,但是我写这个答案不是为了帮助你摆脱你的问题,而是为了可能有类似问题的其他人。希望它可以帮助你解决问题。