UIImagePickerController

UIImagePickerController

-- 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 allocinit];

        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.0fwriteToFile:[self findUniqueSavePathatomically:YES];

    SETIMAGE(image);

CFShow([[NSFileManager defaultManagerdirectoryContentsAtPath:[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 defaultManagerdirectoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);

// NSLog(videoURL);

}

[picker dismissModalViewControllerAnimated:YES];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值