iOS 图片来源有三种方法:
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { UIImagePickerControllerSourceTypePhotoLibrary, //1.从图库中选择 UIImagePickerControllerSourceTypeCamera, //2.直接调用摄像头拍照 UIImagePickerControllerSourceTypeSavedPhotosAlbum //3.从相册中选择 } __TVOS_PROHIBITED;
UIImagePickerController是系统提供的用来获取图片和视频的接口,用UIImagePickerController类来获取图片视频,大体分为以下几个步骤:
1. 实例化UIImagePickerController 2. 设置UIImagePickerController数据来源类型 3. 设置代理,遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate协议 4. 实现代理方法接收选取图片并做处理
在代理中获取我们选中的图片,UIImagePickerControllerDelegate代理中一共三个方法,其中一个3.0已经废弃了,只剩下两个我们需要用的。
//1.当用户选取完成后调用; - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info; //2.当用户取消选取时调用; - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
在这里,我们主要使用第一个代理方法,在选取图片完成后调用,选取的信息都在info中,info 是一个字典。
// 代理方法,获取图片 // 选取的信息都在info中,info 是一个字典。 /** // info dictionary keys UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType __TVOS_PROHIBITED;//指定用户选择的媒体类型 UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage __TVOS_PROHIBITED;//原始图片 UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage __TVOS_PROHIBITED;//修改后的图片 UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect __TVOS_PROHIBITED;//裁剪尺寸 UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL __TVOS_PROHIBITED;//媒体的URL UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL NS_AVAILABLE_IOS(4_1) __TVOS_PROHIBITED;//原件的URL UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata NS_AVAILABLE_IOS(4_1) __TVOS_PROHIBITED;//如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片
下面是具体的代码,只写了核心的部分,当你需要获取系统图片时,执行- (void)startGetPhoto;此方法即可。
//点击更换头像时开始执行此方法 - (void)startGetPhoto { self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", @"拍照", nil]; self.actionSheet.tag = ActionSheetTagAddPhoto; [self.actionSheet showInView:self.view]; } #pragma mark - #pragma mark UIActionSheetDelegate Call Back Implementation - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (0 == buttonIndex) { //打开本地相册 [self localPhoto]; } else if(1 == buttonIndex) { // 开始拍照 [self takePhoto]; } } #pragma mark - #pragma mark UIImagePickerControllerDelegate Call Back Implementation - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片 if ([type isEqualToString:@"public.image"]) { NSString *key = nil; if (picker.allowsEditing) { key = UIImagePickerControllerEditedImage; } else { key = UIImagePickerControllerOriginalImage; } //获取图片 UIImage *image = [info objectForKey:key]; if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { // 固定方向 image = [image fixOrientation];//这个方法是UIImage+Extras.h中方法 //压缩图片质量 image = [self reduceImage:image percent:0.1]; CGSize imageSize = image.size; imageSize.height = 320; imageSize.width = 320; //压缩图片尺寸 image = [self imageWithImageSimple:image scaledToSize:imageSize]; } //上传到服务器 //[self doAddPhoto:image]; //关闭相册界面 [picker dismissViewControllerAnimated:YES completion:^{ }]; } } // 开始拍照 -(void)takePhoto { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; //设置拍照后的图片可被编辑 picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; //先检查相机可用是否 BOOL cameraIsAvailable = [self checkCamera]; if (YES == cameraIsAvailable) { [self presentViewController:picker animated:YES completion:nil]; }else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在iPhone的“设置-隐私-相机”选项中,允许本应用程序访问你的相机。" delegate:self cancelButtonTitle:@"好,我知道了" otherButtonTitles:nil]; [alert show]; } } } // 打开本地相册 -(void)localPhoto { //本地相册不需要检查,因为UIImagePickerController会自动检查并提醒 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //设置选择后的图片可被编辑 picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; } //检查相机是否可用 - (BOOL)checkCamera { NSString *mediaType = AVMediaTypeVideo; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; if(AVAuthorizationStatusRestricted == authStatus || AVAuthorizationStatusDenied == authStatus) { //相机不可用 return NO; } //相机可用 return YES; } //压缩图片质量 -(UIImage *)reduceImage:(UIImage *)image percent:(float)percent { NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage; } //压缩图片尺寸 - (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }