用 UIImagePickerController 选 择、显示图片或视频,主要注意 UIImagePickerController 几 个属性的设置
一:UI 显示样式,显示的格式确定
1: sourceType
@property(nonatomic) UIImagePickerControllerSourceType sourceType
enum {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
typedef NSUInteger UIImagePickerControllerSourceType;
sourceType用来确定用户界面显示的样式:
共三种格式(模拟器上的效果图)
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
![iphone开发常用代码(不断更新) - 勇者之尊 - 等待 iphone开发常用代码(不断更新) - 勇者之尊 - 等待](http://img.blog.163.com/photo/_9IlT-BYrXJwC0VZRpCJMQ==/4510636501789171949.jpg)
UIImagePickerControllerSourceTypeSavedPhotosAlbum
![iphone开发常用代码(不断更新) - 勇者之尊 - 等待 iphone开发常用代码(不断更新) - 勇者之尊 - 等待](http://img.blog.163.com/photo/nZof5OLWemiB-_-qIx7dhQ==/4510636501789171950.jpg)
为了区分是否支持视频格式,一般要用到下面这个函数,以便确定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];
}
实例:
首先要遵守UIImagePickerControllerDelegate协议,然后实现协议的主要方法。
//拍照
- (IBAction)takePictureAction:(id)sender
{
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[self presentViewController:imagePicker animated:YES completion:nil];
}
//选取图片之后
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
StudentStore *sharedStore=[StudentStore sharedStore];
Student *p= [sharedStore currentStudent];
ImageStore *iStore=[ImageStore sharedStore];
NSString *oldKey=[p imagePath];
if(oldKey)
{
//删除老照片
[iStore deleteImageForKey:oldKey];
}
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//生成唯一标志 CF:Core Foundation UUID:唯一标志 Ref:指针
CFUUIDRef newUniqueID=CFUUIDCreate(kCFAllocatorDefault);
//创建相应字符串
CFStringRef newUniqueIDString =CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
NSString *key=(__bridge NSString*)newUniqueIDString;
[p setImagePath:key];
[iStore setImage:image forKey:key];
[self dismissViewControllerAnimated:YES completion:nil];
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);
}