IOS探索之从拍照中获取图像对应的ALAsset
2016-01-07 11:40
197 查看
打开相机
看一下UIImagePickerControllerDelegate结构的声明
其中
便是我们拍照完成之后的一个通知,其中的info携带了有关图片的一些信息,下面代码便是从这个info中抽取有用的信息得到我们需要的图像对应的ALAsset对象指针,
上面assetForURL消息中的resultBlock函数块参数便是回调回来的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对象指针了。
相关文章推荐
- 自定义iOS上双击Home键截屏切换,保护应用程序隐私
- iOS Newbie - Xcode 7 & iOS 9 前篇(非技术)
- iOS应用程序的生命周期
- iOS 百度地图_回调不走_onGetGeoCodeResult不执行
- Mac&iOS Socket
- 浅谈iOS Push 在iOS6/7/8/9中的进化
- IOS 枚举定义
- 微软自拍应用iOS版发布
- iOS类别(Category)和扩展(Extension)
- ios 隐藏顶部状态栏
- IOS后台运行 之 后台播放音乐
- IOS为文字和图片添加阴影效果
- iOS每日一记----------密码输入工具 InputPasswordView
- ios9下,使用storyboard进行多人合作新改变/
- iOS 摇一摇
- IOS开发NSBundle对象使用详解
- iOS 报错问题大搜集(常更)
- ios开发必备第三方库
- iOS数据持久化之NSKeyedArchiver(归档)
- iOS 点击空白处收回键盘