繁体   English   中英

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

[英]Handle with 'UIImagePickerController' and 'allowsEditing' to get perfect square

我怎样才能确保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];
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM