1。 定義 類變數:
UIImagePickerController* picker_library_;
2。實現 UIImagePickerControllerDelegate 這個delegate,還需要UINavigationControllerDelegate 這個代理
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
兩種函數的處理代碼如下:
//3.x 使用者選中圖片後的回調 - (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info { if (picker == picker_camera_) { //如果是 來自照相機的image,那麼先儲存 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]; }
//2.x 使用者選中圖片之後的回調 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { NSMutableDictionary * dict= [NSMutableDictionary dictionaryWithDictionary:editingInfo]; [dict setObject:image forKey:@"UIImagePickerControllerEditedImage"]; //直接調用3.x的處理函數 [self imagePickerController:picker didFinishPickingMediaWithInfo:dict]; }
5。 使用者取消選擇
// 使用者選擇取消 - (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker { [self dismissModalViewControllerAnimated:YES]; [picker release]; }
6。示範樣本
- (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];}