iOS 根据图片URL从本地相册获取图片
最近做一个聊天的项目,需要发送图片后读取本地图片显示到列表里。刚开始的时候,天真的认为可以用SDWebImage直接加载,然后并不能行。
于是在网上搜了搜,如何根据从相册获取的UIImagePickerControllerReferenceURL读取图片, 代码如下:
#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h> @interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
- (IBAction)showImagePickerVC:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
}
弹出图片选择器
- (IBAction)showImagePickerVC:(id)sender { UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init]; imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerVC.allowsEditing = YES; imagePickerVC.delegate = self; [self presentViewController:imagePickerVC animated:YES completion:nil]; }
回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@", info);
NSURL *imagePath = info[@"UIImagePickerControllerReferenceURL"]; if ([[[imagePath scheme] lowercaseString] isEqualToString:@"assets-library"]) { // Load from asset library async
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
@try {
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imagePath
resultBlock:^(ALAsset *asset){
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullScreenImage];
if (iref) {
//进行UI修改
dispatch_sync(dispatch_get_main_queue(), ^{
_imageView.image = [[UIImage alloc] initWithCGImage:iref];
}); } }
failureBlock:^(NSError *error) { NSLog(@"从图库获取图片失败: %@",error); }];
} @catch (NSException *e) {
NSLog(@"从图库获取图片异常: %@", e);
}
}
}); } [picker dismissViewControllerAnimated:YES completion:nil];
}
iOS 根据图片URL从本地相册获取图片的更多相关文章
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...
- ios中摄像头/相册获取图片压缩图片上传服务器方法总结
本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下. 这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...
- iOS 使用AFN 进行单图和多图上传 摄像头/相册获取图片,压缩图片
图片上传时必要将图片进行压缩,不然会上传失败 首先是同系统相册选择图片和视频.iOS系统自带有UIImagePickerController,可以选择或拍摄图片视频,但是最大的问题是只支持单选,由于项 ...
- React Native之图片保存到本地相册(ios android)
React Native之图片保存到本地相册(ios android) 一,需求分析 1,react native保存网络图片到相册,iOS端可以用RN自带的CameraRoll完美解决,但是andr ...
- IOS研究院之打开照相机与本地相册选择图片(六)
原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:IOS研究院之打开照相机与本地相册选择图片(六) Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈. 这篇文章主要学习 ...
- iOS 从相机或相册获取图片并裁剪
今天遇到一个用户头像上传的问题,需要从相册或者相机中读取图片.代码很简单,抽取关键部分,如下: //load user image - (void)UesrImageClicked { UIActio ...
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
- android 照相或从相册获取图片并裁剪
照相或从相册获取图片并裁剪 在android应用中很多时候都要获取图片(例如获取用户的头像)就需要从用户手机上获取图片.可以直接照,也可以从用户SD卡上获取图片,但获取到的图片未必能达到要求.所以要对 ...
- 微信小程序:从本地相册选择图片或使用相机拍照。
wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...
随机推荐
- aix6.1 openssh安装
环境: IBM AIX6.1 1.下载(可以直接从附件中下载): openssl IBM官方网站下载:https://www14.software.ibm.com/webapp/iwm/web/reg ...
- AndroidUI 布局动画-布局内容改变动画
实现一个点击菜单动画添加按钮,点击按钮移除当前按钮的动画效果: <LinearLayout xmlns:android="http://schemas.android.com/apk/ ...
- C# in Depth阅读笔记2:C#2特性
1.方法组转换 c#2支持一个从方法组到兼容委托类型的隐式转换,即如: button.click+=new eventhandler(logevent)可以写成 button.click+=logev ...
- js——cookie
cookie:存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来向访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2. cookie的存储是 ...
- js arguments参数说明
在javascript中,不需要明确指出参数名,就能访问它们.如: function hi(){if(arguments[0]=="andy"){ return;}aler ...
- read和onload jquery.val
$(document).load(); 当web页面以及其附带的资源文件,如CSS,Scripts,图片等,加载完毕后执行此方法.常用于检测页面(及其附带资源)是否加载完毕. $(document). ...
- error1
#include<stdio.h>main(){ int a[10],i,m,n,j; for(i=3;i<10;i++) scanf("%d",&a ...
- leetcode Letter Combinations of a Phone Number python
class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...
- struts2中的表达元素标签使用详解
级联标签是使用:一级下拉框应该使用map对象的key集合作为下拉框元素,二级下了框应该使用一级下拉框对应的选择值自动的弹出待选择的元素值(集合) 页面代码如下:<s:set name=" ...
- Storm并发度和Grouping方式
Storm并发度和Grouping方式 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans ...