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

打开相机

//先设定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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值