3

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

它在我的应用程序中的情况(在 iOS 7 中构建)

在此处输入图像描述

应该如何(WhatsApp - iOS 6)

在此处输入图像描述

我的代码:

- (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];
}
4

1 回答 1

0

要更改捕获的图像的大小,您需要UIImageView在 IB 中设置查看模式以缩放以填充。然后当从库或相机中选择图像时,它会自动调整大小。您确实必须关闭自动布局,因为它不允许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

ImageTestControllerXib 或情节提要中的视图中,放置两个按钮,每个按钮一个,并在 IB 中创建必要的链接。然后放置 aUIImageView以填充视图中您想要的任何大小。单击UIImageView属性检查器的视图部分,将模式更改为缩放以填充。关闭自动布局。尝试将图像添加到您的模拟器只是为了测试它。确保图像是横向尺寸。构建并运行,当您从图像库中选择图像时,它会自动调整为正方形或您设置的任何其他大小UIImageView

我知道您知道上述大部分步骤,但我写这个答案不仅是为了帮助您摆脱您的问题,而且是为了其他可能有类似问题的人。希望它可以帮助你。

于 2013-08-25T15:21:09.560 回答