ios中關於UIImagePickerController的一些知識總結

來源:互聯網
上載者:User

標籤:

記得添加MobileCoreServices.framework

及匯入#import <MobileCoreServices/MobileCoreServices.h>

@interface PPViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

{

    UIImagePickerController *_pickerController;

}

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

 

@end

 

@implementation PPViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    _pickerController = [[UIImagePickerController alloc] init];

    _pickerController.delegate = self;//設定代理

    _pickerController.allowsEditing = YES;//圖片可編輯(放大縮小)

}

- (IBAction)choseImage:(id)sender

{

    // 判斷是否有相機

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        

        //判斷是否能攝像

        if ([[UIImagePickerController availableMediaTypesForSourceType:_pickerController.sourceType] containsObject:(NSString *)kUTTypeMovie])

        {

            _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];

            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相簿",@"相機",@"攝像機", nil];

            sheet.tag = 2;

            [sheet showInView:self.view];

        }

        else

        {

            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相機",@"相簿", nil];

            sheet.tag = 1;

            [sheet showInView:self.view];

        }

    }

    

    else

    {//模擬器

        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"相簿", nil];

        sheet.tag = 3;

        [sheet showInView:self.view];

    }

 

}

#pragma mark- UIActionSheet的代理方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (actionSheet.tag == 1)

    {

        switch (buttonIndex)

        {

            case 0:

            {

                _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

                _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];

                [self presentViewController:_pickerController animated:YES completion:nil];

            }

                break;

            case 1:

            {

                _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];

                [self presentViewController:_pickerController animated:YES completion:nil];

            }

                break;

                

            default:

                break;

        }

 

    }

    if (actionSheet.tag == 2)

    {

        switch (buttonIndex)

        {

            case 0:

            {

                _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

                _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];

                [self presentViewController:_pickerController animated:YES completion:nil];

            }

                break;

            case 1:

            {

                _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//                _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];

                [self presentViewController:_pickerController animated:YES completion:nil];

            }

                break;

            case 2:

            {

                _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

                //        pickController.videoQuality = UIImagePickerControllerQualityTypeLow;//可以選擇圖片品質

                _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];

                [self presentViewController:_pickerController animated:YES completion:nil];

            }

            default:

                break;

        }

    }

    if (actionSheet.tag == 3)

    {

        if (buttonIndex == 0)

        {

            _pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//            _pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];

            [self presentViewController:_pickerController animated:YES completion:nil];

        }

 

    }

}

 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    NSLog(@"========%@",info);//返回的值都在info裡面

    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie])

    {

        _imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:(NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path]]];

    }

    else

    {

        _imageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    }

    [self dismissViewControllerAnimated:YES completion:nil];

    

}

//如果這是一個modalViewController,需要dismiss 它發了個cancel訊息,必須關閉它

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [self dismissViewControllerAnimated:YES completion:nil];

}

/**

 *  {

 UIImagePickerControllerMediaType = "public.image";

 UIImagePickerControllerOriginalImage = "<UIImage: 0x7fe3a3dc2a20> size {1500, 1001} orientation 0 scale 1.000000";

 UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG";

 }

 */

/**

 *  {

 UIImagePickerControllerCropRect = "NSRect: {{0, 0}, {1500, 1003}}";

 UIImagePickerControllerEditedImage = "<UIImage: 0x7fec5c8145b0> size {638, 426} orientation 0 scale 1.000000";

 UIImagePickerControllerMediaType = "public.image";

 UIImagePickerControllerOriginalImage = "<UIImage: 0x7fec5c80d4c0> size {1500, 1001} orientation 0 scale 1.000000";

 UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG";

 }

 */

ios中關於UIImagePickerController的一些知識總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.