码迷,mamicode.com
首页 > 其他好文 > 详细

iphone 怎么使用图片选取器 UIImagePickerController

时间:2015-05-07 11:56:14      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

?

iphone 怎么使用图片选取器 UIImagePickerController

1。 定义 类变量:

?? ? ??UIImagePickerController*?picker_library_;

?

?

2。实现?UIImagePickerControllerDelegate ? 这个delegate

?

?

3。 以模态的方式,显示 图片选取器

?? ? ?

  1. picker_library_?=?[[UIImagePickerController?alloc]?init];??
  2. ????picker_library_.sourceType?=?UIImagePickerControllerSourceTypePhotoLibrary;??
  3. ????picker_library_.allowsEditing?=?YES;??
  4. ????picker_camera_.allowsImageEditing=YES;??
  5. ????picker_library_.delegate?=?self;??
  6. ????[self?presentModalViewController:?picker_library_??
  7. ????????????????????????????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

?

?

?? ? 两种函数的处理代码如下:

?? ?

  1. //3.x??用户选中图片后的回调??
  2. -?(void)imagePickerController:?(UIImagePickerController?*)picker???
  3. didFinishPickingMediaWithInfo:?(NSDictionary?*)info??
  4. {??
  5. ????if?(picker?==?picker_camera_)???
  6. ????{??
  7. ????????//如果是?来自照相机的image,那么先保存??
  8. ????????UIImage*?original_image?=?[info?objectForKey:@"UIImagePickerControllerOriginalImage"];??
  9. ????????UIImageWriteToSavedPhotosAlbum(original_image,?self,???
  10. ???????????????????????????????????????@selector(image:didFinishSavingWithError:contextInfo:),???
  11. ???????????????????????????????????????nil);??
  12. ????}??
  13. ??????
  14. ????//获得编辑过的图片??
  15. ????UIImage*?image?=?[info?objectForKey:?@"UIImagePickerControllerEditedImage"];??
  16. ??????
  17. ??????
  18. ????[self?dismissModalViewControllerAnimated:YES];??
  19. ????[picker?release];??
  20. }??
?

?

?

?? ? ?

  1. //2.x??用户选中图片之后的回调??
  2. -?(void)imagePickerController:(UIImagePickerController?*)picker?didFinishPickingImage:(UIImage?*)image?editingInfo:(NSDictionary?*)editingInfo??
  3. {??
  4. ????NSMutableDictionary?*?dict=?[NSMutableDictionary?dictionaryWithDictionary:editingInfo];??
  5. ??????
  6. ????[dict?setObject:image?forKey:@"UIImagePickerControllerEditedImage"];??
  7. ??????
  8. ????//直接调用3.x的处理函数??
  9. ????[self?imagePickerController:picker?didFinishPickingMediaWithInfo:dict];??
  10. }??
?

?

?

?

??5。 用户取消选择

?? ? ?

  1. //?用户选择取消??
  2. -?(void)?imagePickerControllerDidCancel:?(UIImagePickerController?*)picker??
  3. {??
  4. ????[self?dismissModalViewControllerAnimated:YES];??
  5. ????[picker?release];??
  6. }??

?

iphone 怎么使用图片选取器 UIImagePickerController

标签:

原文地址:http://www.cnblogs.com/allen123/p/4484324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!