1。 定义 类变量:
?? ? ??UIImagePickerController*?picker_library_;
?
?
2。实现?UIImagePickerControllerDelegate ? 这个delegate
?
?
3。 以模态的方式,显示 图片选取器
?? ? ?
- picker_library_?=?[[UIImagePickerController?alloc]?init];??
- ????picker_library_.sourceType?=?UIImagePickerControllerSourceTypePhotoLibrary;??
- ????picker_library_.allowsEditing?=?YES;??
- ????picker_camera_.allowsImageEditing=YES;??
- ????picker_library_.delegate?=?self;??
- ????[self?presentModalViewController:?picker_library_??
- ????????????????????????????animated:?YES];??
?
?
?? ? ?其中,sourceType 指定了 几种 图片的来源:
?? ? ?UIImagePickerControllerSourceTypePhotoLibrary:表示显示所有的照片
?? ? ?UIImagePickerControllerSourceTypeCamera:表示从摄像头选取照片
?? ? ?UIImagePickerControllerSourceTypeSavedPhotosAlbum:表示仅仅从相册中选取照片。
?
?? ? ?allowEditing和allowsImageEditing ?设置为YES,表示 允许用户编辑图片,否则,不允许用户编辑。
?
?
4。 当用户选择一个图片以后,有可能调用两种不同的函数,根据版本的不同。所以,如果要同时支持高版本和低版本的兼容性,那么就
?? ? ?要处理两种函数。
?
?? ? ?//3.x??用户选中图片后的回调
- (void)imagePickerController: (UIImagePickerController?*)picker?
didFinishPickingMediaWithInfo: (NSDictionary?*)info
?
?
?? ??//2.x??用户选中图片之后的回调
- (void)imagePickerController:(UIImagePickerController?*)picker didFinishPickingImage:(UIImage?*)image editingInfo:(NSDictionary?*)editingInfo
?
?
?? ? 两种函数的处理代码如下:
?? ?
- ??
- -?(void)imagePickerController:?(UIImagePickerController?*)picker???
- didFinishPickingMediaWithInfo:?(NSDictionary?*)info??
- {??
- ????if?(picker?==?picker_camera_)???
- ????{??
- ??????????
- ????????UIImage*?original_image?=?[info?objectForKey:@"UIImagePickerControllerOriginalImage"];??
- ????????UIImageWriteToSavedPhotosAlbum(original_image,?self,???
- ???????????????????????????????????????@selector(image:didFinishSavingWithError:contextInfo:),???
- ???????????????????????????????????????nil);??
- ????}??
- ??????
- ??????
- ????UIImage*?image?=?[info?objectForKey:?@"UIImagePickerControllerEditedImage"];??
- ??????
- ??????
- ????[self?dismissModalViewControllerAnimated:YES];??
- ????[picker?release];??
- }??
?
?
?
?? ? ?
- ??
- -?(void)imagePickerController:(UIImagePickerController?*)picker?didFinishPickingImage:(UIImage?*)image?editingInfo:(NSDictionary?*)editingInfo??
- {??
- ????NSMutableDictionary?*?dict=?[NSMutableDictionary?dictionaryWithDictionary:editingInfo];??
- ??????
- ????[dict?setObject:image?forKey:@"UIImagePickerControllerEditedImage"];??
- ??????
- ??????
- ????[self?imagePickerController:picker?didFinishPickingMediaWithInfo:dict];??
- }??
?
?
?
?
??5。 用户取消选择
?? ? ?
- ??
- -?(void)?imagePickerControllerDidCancel:?(UIImagePickerController?*)picker??
- {??
- ????[self?dismissModalViewControllerAnimated:YES];??
- ????[picker?release];??
- }??
?