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];
}
时间: 2024-07-31 23:05:54

iOS 根据图片URL从本地相册获取图片的相关文章

android 调用系统相机获取图片、调用系统相册获取图片,并对图片进行截取

打开系统相册获取图片并截取,代码相对简单 1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null); 2 intent.setType("image/*"); 3 intent.putExtra("crop", "true"); 4 5 //WIDTH 和 HEIGHT指的是截取框的宽高比例,如设WIDTH = 1,HEIGHT = 1,截取框就为正方形 6 intent.putEx

调去系统照相机或者从本地相册获取图片.

1.首先要遵守三个协议 UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate 2.主要代码 //选择头像 - (IBAction)handleSelectImage:(id)sender { self.myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"

ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结

相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直接从应用程序访问,只能通过终端用户去选择和使用相册图片 应用程序包 应用程序包可能会将图像与可执行程序.Info.plist文件和其他资源一同存储.我们可以通过本地文件路径来读取这些基于包的图像并在应用程序中显示它们. 沙盒 借助沙盒,我们可以把图片存储到Documents.Library.tmp文

iOS 使用AFN 进行单图和多图上传 摄像头/相册获取图片,压缩图片

图片上传时必要将图片进行压缩,不然会上传失败 首先是同系统相册选择图片和视频.iOS系统自带有UIImagePickerController,可以选择或拍摄图片视频,但是最大的问题是只支持单选,由于项目要求需要支持多选,只能自己自定义.获取系统图库的框架有两个,一个是ALAssetsLibrary,兼容iOS低版本,但是在iOS9中是不建议使用的:另一个是PHAsset,但最低要求iOS8以上.兼容到iOS7,可以选择了ALAssetsLibrary 现在我们先说选择一张图的情况 一.单图多图上

iOS 从相机或相册获取图片并裁剪

/load user image - (void)UesrImageClicked { UIActionSheet *sheet; // 判断是否支持相机 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancel

Android从相册获取图片

应用中经常会有去相册获取图片的需求,每次总是会忘,现在这做个记录,以便以后查阅. 从应用去相册获取图片主要有以下几个步骤: 1.设置intent调用系统相册 2.进入相册选取图片,如果相册没有满意的图片,可以使用相机照相获取,最后返回数据到应用 3.应用获取图片后,调用裁剪程序对图片进行裁剪 4.返回裁剪的图片并显示出来 一.设置intent调用系统相册,并进入相册选取图片 /** * 调用相册 */ private void goAlbums() { Intent intent = new I

iOS--将图片保存至本地相册

今天做聊天,领导说对方给我发一个图片,我要保存到本地,IOS的UIKit Framework提供了UIImageWriteToSavedPhotosAlbum方法对图像进行保存,该方法会将image保存至用户的相册中: 上代码: void UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo ); 参数说明: image : 需

从相册获取图片及调用相机拍照获取图片,最后上传图片到服务器

调用相机拍照获取图片: 跳转到到拍照界面: Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //下面这句指定调用相机拍照后的照片存储的路径 mSzImageFileName = Long.toString(System.currentTimeMillis()) + ".png"; takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new F

从相机相册获取图片裁剪后用于评论晒图或更换背景图

这是我人生中写的第一篇博客,是否要纪念一下这一刻(2016.09.01 16:52).其实关于写博客,老早就有这种写法,首先觉得他能够帮我总结我学到的和用过的技术,其次还能帮助那些和我有一样需求的人,我也是很开心啊,但是至于为什么现在才写第一篇,首先没有想好写什么,然后前段时间也确实比较忙.是不是那些来观技术的人已经想骂人了啊...啊哦!原谅我第一次写博客的激动心情吧! 废话不多说,开始我们的问题吧,首先因为我做了两次关于调用相机和相册获取图片的功能,觉得很有必要总结一下,下面我将从这两个功能出