iOS 根據圖片URL從本地相簿擷取圖片

來源:互聯網
上載者:User

標籤:

最近做一個聊天的項目,需要發送圖片後讀取本地圖片顯示到列表裡。剛開始的時候,天真的認為可以用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從本地相簿擷取圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.