typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType)
{
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
} ```
`typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {
UIImagePickerControllerQualityTypeHigh = 0,
// highest quality
UIImagePickerControllerQualityTypeMedium = 1,
// medium quality, suitable for transmission via Wi-Fi
UIImagePickerControllerQualityTypeLow = 2,
// lowest quality, suitable for tranmission via cellular network
UIImagePickerControllerQualityType640x480,
// VGA quality
UIImagePickerControllerQualityTypeIFrame1280x720,
UIImagePickerControllerQualityTypeIFrame960x540,
}`
`@interface UIImagePickerController : UINavigationController <NSCoding>`
`+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType; `
`+ (nullable NSArray<NSString *> *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType;`
`+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice `
`+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice `
`+ (nullable NSArray<NSNumber *> *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice`
`@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;`
`@protocol UIImagePickerControllerDelegate<NSObject>`
`- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;`
`- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;`
该类继承自UINavigationController类
调用摄像头或相册来获取图片和视频时都可以用此类解决
######iOS 获取图片有三种方法
***
1 直接调用摄像头拍照
2 从相册中选择
3 从图库中选择
UIImagePickerController 是系统提供的用来获取图片和视频的接口;
用UIImagePickerController 类来获取图片视频;
大体分为以下几个步骤:
初始化UIImagePickerController 类
设置UIImagePickerController 实例的数据来源类型(下面解释)�;
设置设置代理
如果需要做图片修改的话设置allowsEditing =yes
数据来源类型一共有三种:
<code>
enum {
UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库
UIImagePickerControllerSourceTypeCamera ,//来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
};
</code>
在用这些来源的时候最好检测以下设备是否支持;
<code>
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"支持相机");
}
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@"支持图库");
}
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@"支持相片库");
}
</code>
调用摄像头来获取资源
<code>
-(void)viewDidLoad {
[super viewDidLoad];
picker = [[UIImagePickerController alloc]init];
picker.view.backgroundColor = [UIColor orangeColor];
UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
picker.sourceType = sourcheType;
picker.delegate = self;
picker.allowsEditing = YES;
}
</code>
上面只是实例了UIImagePickerController及其属性 在需要获取图片的时候需要弹出窗口调用
`[self presentViewController:picker animated:YES completion:nil]; `
我们还需要代理来获取我们选中的图片
######UIImagePickerControllerDelegate
***
有个3.0时 废弃略过
`- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;`
当用户选取完成后调用;
`- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;`
当用户取消选取时调用;
<code>
NSString \*const UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
NSString \*const UIImagePickerControllerOriginalImage ;原始图片
NSString \*const UIImagePickerControllerEditedImage ;修改后的图片
NSString \*const UIImagePickerControllerCropRect ;�裁剪尺寸
NSString \*const UIImagePickerControllerMediaURL ;媒体的URL
NSString \*const UIImagePickerControllerReferenceURL ;原件的URL
NSString \*const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
</code>
http://kayosite.com/ios-development-and-detail-of-photo-framework.html/comment-page-1
转自:[iOS 获取图片](http://blog.csdn.net/u010123208/article/details/39493535)
网友评论