2011年3月31日 星期四

【轉】 【iPhone】UIImagePickerController 查看圖片,視頻,錄像


轉載自 hh20040410
最終編輯 hh20040410
UIImagePickerController -- the only sanctioned way to get videos/movies or images/pictures on supported devices UIImagePickerController
選擇、顯示圖片或視頻,
主要注意 UIImagePickerController 幾個屬性的設置 一:UI 顯示樣式,顯示的格式確定 1: sourceType
  @property(nonatomic) UIImagePickerControllerSourceType sourceType

  enum
 {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
}; typedef NSUInteger UIImagePickerControllerSourceType;

  sourceType用來 確定用戶界面顯示的樣式: 共三種格式(模擬器上的效果圖)
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
  UIImagePickerControllerSourceTypeSavedPhotosAlbum

  為了區分是否支持視頻格式,一般要用到下面這個函數,以便確定mediaTypes。
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

  2: mediaTypes
  @property(nonatomic,copy) NSArray *mediaTypes

  mediaTypes用來確定再picker裡顯示那些類型的多媒體文件,圖片? 視頻?


+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType

  二:選取動作處理
UIImagePickerControllerDelegate 


通過代理來完成用戶在選中圖片,或者choose視頻時的處理方式: 共有三個可選的代理方法
– imagePickerController:didFinishPickingMediaWithInfo:
– imagePickerControllerDidCancel: 
– imagePickerController:didFinishPickingImage:editingInfo:
Deprecated in iPhone OS 3.0

  - (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *) info
info中包括選取的照片,視頻的主要信息 NSString *const UIImagePickerControllerMediaType;選取的類型 public.image public.movie NSString *const UIImagePickerControllerOriginalImage; 修改前的UIImage object. NSString *const UIImagePickerControllerEditedImage; 修改後的UIImage object. NSString *const UIImagePickerControllerCropRect; 原始圖片的尺寸NSValue object containing a CGRect data type NSString *const UIImagePickerControllerMediaURL; 視頻在文件系統中的NSURL地址 保存視頻主要時通過獲取其NSURL然後轉換成 NSData 實例代碼如下:
- ( void ) pickImage: ( id ) sender
{
UIImagePickerController *ipc = [[ UIImagePickerController alloc ] init ];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary ]){
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
ipc.mediaTypes =[ UIImagePickerController availableMediaTypesForSourceType :ipc. sourceType ];
}
ipc.delegate = self ;
ipc. allowsImageEditing = NO ;
[ self presentModalViewController :ipc animated : YES ];
}
- ( void )imagePickerController:( UIImagePickerController *)picker
didFinishPickingMediaWithInfo:( NSDictionary *)info
{
NSString *mediaType = [info objectForKey : UIImagePickerControllerMediaType ];
if ([mediaType isEqualToString : @"public.image" ]){
// UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *image = [info objectForKey : @"UIImagePickerControllerOriginalImage" ];
NSLog ( @"found an image" );
[ UIImageJPEGRepresentation (image, 1.0f ) writeToFile :[ self findUniqueSavePath ] atomically : YES ];
SETIMAGE (image);
CFShow ([[ NSFileManager defaultManager ] directoryContentsAtPath :[ NSHomeDirectory () stringByAppendingString : @"/Documents" ]]);
}
else if ([mediaType isEqualToString : @"public.movie" ]){
NSURL *videoURL = [info objectForKey : UIImagePickerControllerMediaURL ];
NSLog ( @"found a video" );
NSData *webData = [ NSData dataWithContentsOfURL :videoURL];
//NSData *video = [[NSString alloc] initWithContentsOfURL:videoURL];
[webData writeToFile :[ self findUniqueMoviePath] atomically : YES ];
CFShow ([[ NSFileManager defaultManager ] directoryContentsAtPath :[ NSHomeDirectory () stringByAppendingString : @"/Documents" ]]);
// NSLog(videoURL);
}
[picker dismissModalViewControllerAnimated : YES ];
}

沒有留言:

張貼留言