gpt4 book ai didi

iphone - 用 'UIImagePickerController' 和 'allowsEditing' 处理以获得完美的正方形

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:40 26 4
gpt4 key购买 nike

我如何确保 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];
}

最佳答案

要更改捕获图像的大小,您需要在 IB 的 UIImageView 中设置 View 模式以缩放以填充。然后当从库或相机中选择图像时,它会自动调整大小。你必须关闭自动布局,因为它不允许调整 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 或 Storyboard中的 View 中,放置两个按钮,一个用于每个操作,并在 IB 中创建必要的链接。然后放置一个 UIImageView 来填充 View 中您想要的任何大小。单击 UIImageView 并在属性检查器的 View 部分将模式更改为缩放以填充。关闭自动布局。尝试将图像添加到您的模拟器只是为了测试它。确保图像为横向尺寸。构建并运行,当您从图像库中选择图像时,它会自动调整为正方形或您设置的任何其他尺寸 UIImageView

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

关于iphone - 用 'UIImagePickerController' 和 'allowsEditing' 处理以获得完美的正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com