iOS 根据图片URL从本地相册获取图片
生活随笔
收集整理的這篇文章主要介紹了
iOS 根据图片URL从本地相册获取图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近做一個聊天的項目,需要發(fā)送圖片后讀取本地圖片顯示到列表里。剛開始的時候,天真的認為可以用SDWebImage直接加載,然后并不能行。
于是在網(wǎng)上搜了搜,如何根據(jù)從相冊獲取的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];}?回調(diào)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {NSLog(@"%@", info);NSURL *imagePath = info[@"UIImagePickerControllerReferenceURL"];if ([[[imagePath scheme] lowercaseString] isEqualToString:@"assets-library"]) {// Load from asset library asyncdispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{@autoreleasepool {@try {ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];[assetslibrary assetForURL:imagePathresultBlock:^(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]; }?
轉(zhuǎn)載于:https://www.cnblogs.com/pretty-guy/p/4548761.html
總結(jié)
以上是生活随笔為你收集整理的iOS 根据图片URL从本地相册获取图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js验证input输入框(字母,数字,符
- 下一篇: mac 安装使用 webp 来压缩图片