簡體   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