iOS 從拍照,圖庫,相冊獲取圖片
iOS 獲取圖片有三種方法
1 直接調用攝像頭拍照
2 從相冊中選擇
3 從圖庫中選擇
UIImagePickerController 是系統提供的用來獲取圖片和視頻的接口;
用UIImagePickerController 類來獲取圖片視頻;
大體分為以下幾個步驟:
- 初始化UIImagePickerController 類
- 設置UIImagePickerController 實例的數據來源類型(下面解釋);
- 設置設置代理
- 如果需要做圖片修改的話設置allowsEditing =yes
數據來源類型一共有三種:
enum { UIImagePickerControllerSourceTypePhotoLibrary ,//來自圖庫 UIImagePickerControllerSourceTypeCamera ,//來自相機 UIImagePickerControllerSourceTypeSavedPhotosAlbum //來自相冊 };
在用這些來源的時候最好檢測以下設備是否支持;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"支持相機"); } if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { NSLog(@"支持圖庫"); } if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { NSLog(@"支持相片庫"); }
調用攝像頭來獲取資源
- (void)viewDidLoad { [super viewDidLoad]; picker = [[UIImagePickerController alloc]init]; picker.view.backgroundColor = [UIColor orangeColor]; UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera; picker.sourceType = sourcheType; picker.delegate = self; picker.allowsEditing = YES; }
上面只是實例了UIImagePickerController及其屬性 在需要獲取圖片的時候需要彈出窗口調用
[self presentViewController:picker animated:YES completion:nil];
我們還需要代理來獲取我們選中的圖片
UIImagePickerControllerDelegate
代理中一共三個方法 其中一個3.0 已經廢棄了,只剩下兩個我們需要用的- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
當用戶選取完成后調用;- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
當用戶取消選取時調用;- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
選取的信息都在info中
info 是一個字典
字典中得鍵
NSString *const UIImagePickerControllerMediaType ;指定用戶選擇的媒體類型(文章最后進行擴展) NSString *const UIImagePickerControllerOriginalImage ;原始圖片 NSString *const UIImagePickerControllerEditedImage ;修改后的圖片 NSString *const UIImagePickerControllerCropRect ;裁剪尺寸 NSString *const UIImagePickerControllerMediaURL ;媒體的URL NSString *const UIImagePickerControllerReferenceURL ;原件的URL NSString *const UIImagePickerControllerMediaMetadata;當來數據來源是照相機的時候這個值才有效
UIImagePickerController 的更多參數參考http://blog.sina.com.cn/s/blog_68edaff101019ppe.html
代理中得功能參考 http://bbs.9ria.com/forum.php?mod=viewthread&tid=244453
UIImagePickerControllerMediaType 包含著KUTTypeImage 和KUTTypeMovie
KUTTypeImage 包含
const CFStringRef kUTTypeImage ;抽象的圖片類型 const CFStringRef kUTTypeJPEG ; const CFStringRef kUTTypeJPEG2000 ; const CFStringRef kUTTypeTIFF ; const CFStringRef kUTTypePICT ; const CFStringRef kUTTypeGIF ; const CFStringRef kUTTypePNG ; const CFStringRef kUTTypeQuickTimeImage ; const CFStringRef kUTTypeAppleICNS const CFStringRef kUTTypeBMP; const CFStringRef kUTTypeICO;
KUTTypeMovie 包含
const CFStringRef kUTTypeAudiovisualContent ;抽象的聲音視頻 const CFStringRef kUTTypeMovie ;抽象的媒體格式(聲音和視頻) const CFStringRef kUTTypeVideo ;只有視頻沒有聲音 const CFStringRef kUTTypeAudio ;只有聲音沒有視頻 const CFStringRef kUTTypeQuickTimeMovie ; const CFStringRef kUTTypeMPEG ; const CFStringRef kUTTypeMPEG4 ; const CFStringRef kUTTypeMP3 ; const CFStringRef kUTTypeMPEG4Audio ; const CFStringRef kUTTypeAppleProtectedMPEG4Audio;
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!