-- the only sanctioned way to get videos/movies or images/pictures on supported devices
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
用 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];
}