ios UIImagePickerController簡單說明

來源:互聯網
上載者:User

標籤:

首先,VC中添加#import <MobileCoreServices/MobileCoreServices.h> 使用(NSString *) kUTTypeImage定義在其中

判斷是否授權:

 NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudioAVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];

    AVAuthorizationStatusNotDetermined = 0,//未明確
    AVAuthorizationStatusRestricted,//受限
    AVAuthorizationStatusDenied,//不允許
    AVAuthorizationStatusAuthorized//允許

第一種使方法照相機:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//添加代理imagePicker.delegate = self;
//是否允許編輯imagePicker.allowsEditing = YES;
//資源類型imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//media類型imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
//轉跳[self presentModalViewController:imagePicker animated:YES];

第二種使用攝像機:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//media類型imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
//視頻品質imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;[self presentModalViewController:imagePicker animated:YES];

第三種使用相簿取圖片類型:

 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage, nil]; [self presentModalViewController:imagePicker animated:YES];

第四種使用相簿取視頻類型:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;imagePicker.allowsEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];[self presentModalViewController:imagePicker animated:YES];

代理:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{  if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
      //擷取照片的原圖        UIImage* original = [info objectForKey:UIImagePickerControllerOriginalImage];        //擷取圖片裁剪的圖        UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];        //擷取圖片裁剪後,剩下的圖        UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect];        //擷取圖片的url        NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL];        //擷取圖片的metadata資料資訊        NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];        //如果是拍照的照片,則需要手動儲存到本地,系統不會自動儲存拍照成功後的照片       // UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  } else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {    NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];    self.fileData = [NSData dataWithContentsOfFile:videoPath];
  }  [picker dismissModalViewControllerAnimated:YES];}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissModalViewControllerAnimated:YES];}

 

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.