IOS探索之从拍照中获取图像对应的ALAsset

来源:互联网 发布:sql中decode 编辑:程序博客网 时间:2024/06/03 15:20

打开相机

//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;//    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {//        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//    }    //sourceType = UIImagePickerControllerSourceTypeCamera; //照相机    //sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库    //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化    picker.delegate = self;    picker.allowsEditing = YES;//设置可编辑    picker.sourceType = sourceType;    [self presentModalViewController:picker animated:YES];//进入照相界面    [picker release];

看一下UIImagePickerControllerDelegate结构的声明

@protocol UIImagePickerControllerDelegate<NSObject>@optional// The picker does not dismiss itself; the client dismisses it in these callbacks.// The delegate will receive one or the other, but not both, depending whether the user// confirms or cancels.- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0);- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;@end

其中
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
便是我们拍照完成之后的一个通知,其中的info携带了有关图片的一些信息,下面代码便是从这个info中抽取有用的信息得到我们需要的图像对应的ALAsset对象指针,

- (void)saveImageToPhotos:(UIImage*)savedImage{    if(nil == _library)         _library = [[ALAssetsLibrary alloc] init];    __weak __typeof__(self) weakSelf = self;    [_library writeImageToSavedPhotosAlbum:[savedImage CGImage] orientation:(ALAssetOrientation)[savedImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{            [weakSelf.library assetForURL:assetURL resultBlock:^(ALAsset *asset){//这里的asset便是我们所需要的图像对应的ALAsset了                dispatch_async(dispatch_get_main_queue(),^{                //以下代码纯属个人业务代码,可以不需要关心                    NSMutableDictionary *mediaInfo = [[NSMutableDictionary alloc] init];                    NSMutableArray* assets = [NSMutableArray array];                    [assets addObject:asset];                    [mediaInfo setObject:assets forKey:kMultiMediaData];                    [mediaInfo setObject:[NSNumber numberWithInt:EFilterTypePhotos                                          ] forKey:kMultiMediaFilter];                                        if (self.mediaReViewViewController) {                        [self.mediaReViewViewController addPhotoInfo:mediaInfo];                    }                    [[YYViewControllerCenter currentVisiableRootViewController] dismissViewControllerAnimated:NO completion:^{}];                });            }failureBlock:^(NSError *error) {//                [[YYViewControllerCenter currentVisiableRootViewController] dismissViewControllerAnimated:NO completion:^{}];            }];        });    }];}

上面assetForURL消息中的resultBlock函数块参数便是回调回来的ALAsset对象指针了。


0 0
原创粉丝点击