1 照相机的使用
1) 控制器的使用 UIImagePickerController
7.0系统判断相机是否有使用权限
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied) {
// 相机权限问题界面 (判断是相机还是相册)
}
跳转页面
UIImagePickerController * pickImage = [[UIImagePickerController alloc]init];
pickImage.sourceType = UIImagePickerControllerSourceTypeCamera;// 类型
pickImage.delegate = self;
[self.view.window.rootViewController presentViewController:pickImage animated:YES completion:nil];
拍照完的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];// 拍摄完毕 保存图片
}
2 相册的使用
1) 相机是否可以使用
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {
CLAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) {
}
}
2)获取相册的图片
ALAssetsLibrary * lib = [[ALAssetsLibrary alloc]init];
[lib enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// 获取相册列表
if(group != nil){
NSString * groupName = [group valueForProperty:ALAssetsGroupPropertyName];// 相册名字
libraryData.albumName = groupName;
libraryData.posterImage = group.posterImage;
NSMutableArray * photoArray = [[NSMutableArray alloc]init];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {// 相册照片列表
if(result!= nil){
NSString * assetType = [result valueForProperty:ALAssetPropertyType];
if([assetType isEqualToString:ALAssetTypePhoto]){
[photoArray addObject:result];
}else if([assetType isEqualToString:ALAssetTypeVideo]){
}else if ([assetType isEqualToString:ALAssetTypeUnknown]){
}
}
}];
if(photoArray.count > 0)
{
libraryData.group = group;
libraryData.photoArray = photoArray;
// 存放图片
if ([groupName isEqualToString:@"相机胶卷"]) {
[self.photoLibrary insertObject:libraryData atIndex:0];
} else {
[self.photoLibrary addObject:libraryData];
}
}
}
// init tableview
} failureBlock:^(NSError *error) {
}];
3)处理图片
可以添加通知来存放图片
-(void)selectImageFromPhotoLibraryFinishNotification:(NSNotification *)sender
{
if([[sender object] isKindOfClass:[NSMutableArray class]]){
NSMutableArray * array = (NSMutableArray *)[sender object];
for (ALAsset * asset in array) {
// 获取图片
ALAssetRepresentation * representation = asset.defaultRepresentation;
CGImageRef imageref = [representation fullScreenImage];
UIImage * image = [UIImage imageWithCGImage:imageref];
if ([image isKindOfClass:[UIImage class]]) {
}
}
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_MYPHOTO_LIBRARY_BACK object:nil];
}