iOS根据URL获得手机中的图片

//根据路径获得相册内的图片  imagePath 就是你已经拥有的图片路径

                        NSURL *imageUrl = [NSURL URLWithString:@"imagePath"];

                        ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];

                        [assetslibrary assetForURL:imageUrl resultBlock:^(ALAsset *asset) {


                            ALAssetRepresentation *rep = [asset defaultRepresentation];

                            CGImageRef iref = [rep fullScreenImage];

                            if (iref)

                            {

                                //获得图片

                                dispatch_sync(dispatch_get_main_queue(), ^{

                                    printerImg = [[UIImage alloc] initWithCGImage:iref];

                                });

                            }


                        } failureBlock:^(NSError *error) {


                            NSLog(@"从图库获取图片失败: %@",error);

                        }];


如果遇到发现拿到的照片一直为nil,有可能是获取ALAsset对象的ALAssetsLibrary对象销毁后,ALAsset对象就也会自动销毁,强持有ALAsset对象也不行,可以尝试创建一个用static修饰的ALAssetsLibrary对象 。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容