UIImagePickerControllerSourceTypeSavedPhotosAlbum
为了区分是否支持所需引用的sourceType,一般要用到下面这个函数,以便确定sourceType。
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
2: mediaTypes
@property(nonatomic,copy) NSArray *mediaTypes
mediaTypes用来确定再picker里显示那些类型的多媒体文件,图片?视频?
+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType
UIImagePikerController的一些属性和方法:
@property(nonatomic) UIImagePickerControllerSourceType sourceType;
@property(nonatomic,copy) NSArray *mediaTypes; //是否允许对获得的图片进行编辑,default value is NO.
@property(nonatomic) BOOL allowsEditing
@property(nonatomic) BOOL allowsImageEditing
//视频最大的时间长度
@property(nonatomic) NSTimeInterval videoMaximumDuration
//拍摄照片的清晰度,只有在照相机模式下可用
enum {
UIImagePickerControllerQualityTypeHigh = 0, // highest quality
UIImagePickerControllerQualityType640x480 = 3, // VGA quality
UIImagePickerControllerQualityTypeMedium = 1, // medium quality, suitable for transmission via Wi-Fi
UIImagePickerControllerQualityTypeLow = 2 // lowest quality, suitable for tranmission via cellular network
};
typedef NSUInteger UIImagePickerControllerQualityType;
默认UIImagePickerControllerQualityTypeMedium
@property(nonatomic) UIImagePickerControllerQualityType videoQuality
//是否显示照相机其他控件,默认yes
@property(nonatomic) BOOL showsCameraControls
//类似相框
@property(nonatomic,retain) UIView *cameraOverlayView
@property(nonatomic) CGAffineTransform cameraViewTransform
//可以设置照相机的模式,照相还是录视频,默认照相模式。
enum {
UIImagePickerControllerCameraCaptureModePhoto,
UIImagePickerControllerCameraCaptureModeVideo
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode
//设置哪个引用摄像头,前置还是后置摄像头
@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice
//设置闪光灯模式
enum {
UIImagePickerControllerCameraFlashModeOff = -1,
UIImagePickerControllerCameraFlashModeAuto = 0,
UIImagePickerControllerCameraFlashModeOn = 1
};
typedef NSInteger UIImagePickerControllerCameraFlashMode;
@property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode
二:选取动作处理
UIImagePickerControllerDelegate
通过代理来完成用户在选中图片,或者choose视频时的处理方式:
共有三个可选的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;
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
保存图片、视频的方法。
// Adds a photo to the saved photos album. The optional completionSelector should have the form:
// - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
// Is a specific video eligible to be saved to the saved photos album?
UIKIT_EXTERN BOOL UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_1);
// Adds a video to the saved photos album. The optional completionSelector should have the form:
// - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void //*)contextInfo;
UIKIT_EXTERN void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_1);
实例代码如下:
实例一:
- (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];
}
实例二:主要介绍怎么修改获取图片的方法
加一个图片处理方法的类:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
CGFloat scale = 100/selectedImage.size.height;
UIImage *newpetimage = [selectedImage scaleToSize:CGSizeMake(selectedImage.size.width*scale, selectedImage.size.height*scale)];
NSLog(@"imagew = %f,h = %f",newpetimage.size.width,newpetimage.size.height);
}