IOS成长之路-调用照相机和相册功能

打开相机:

  1. //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库  
  2.     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;  
  3. //    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {  
  4. //        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  5. //    }  
  6.     //sourceType = UIImagePickerControllerSourceTypeCamera; //照相机  
  7.     //sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库  
  8.     //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片  
  9.     UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化  
  10.     picker.delegate = self;  
  11.     picker.allowsEditing = YES;//设置可编辑  
  12.     picker.sourceType = sourceType;  
  13.     [self presentModalViewController:picker animated:YES];//进入照相界面  
  14.     [picker release];  


打开相册:(区分pad和iphone)

for iphone:

  1. UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];  
  2.     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {  
  3.         pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  4.         //pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
  5.         pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];  
  6.           
  7.     }  
  8.     pickerImage.delegate = self;  
  9.     pickerImage.allowsEditing = NO;  
  10.     [self presentModalViewController:pickerImage animated:YES];  
  11.     [pickerImage release];  


for ipad:

  1. UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  2.     //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片  
  3.     UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
  4.     picker.delegate = self;  
  5.     picker.allowsEditing = NO;//是否允许编辑  
  6.     picker.sourceType = sourceType;  
  7.     /* 
  8.      如果从一个导航按钮处呈现,使用: 
  9.      presentPopoverFromBarButtonItem:permittedArrowDirections:animated:; 
  10.      如果要从一个视图出呈现,使用: 
  11.      presentPopoverFromRect:inView:permittedArrowDirections:animated: 
  12.       
  13.      如果设备旋转以后,位置定位错误需要在父视图控制器的下面方法里面重新定位: 
  14.      didRotateFromInterfaceOrientation:(在这个方法体里面重新设置rect) 
  15.      然后再次调用: 
  16.      - (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated              
  17.      */  
  18.     //UIPopoverController只能在ipad设备上面使用;作用是用于显示临时内容,特点是总是显示在当前视图最前端,当单击界面的其他地方时自动消失。  
  19.     UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:picker];  
  20.     self.imagePicker = popover;  
  21.     //permittedArrowDirections 设置箭头方向  
  22.     [self.imagePicker presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];  
  23.     [picker release];  
  24.     [popover release];  


点击相册中的图片 货照相机照完后点击use  后触发的方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info


点击cancel 调用的方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker


例子下载地址:http://download.csdn.net/download/like7xiaoben/4958356

本文转自......

IOS成长之路-调用照相机和相册功能,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。