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

2018-02-08  本文已影响0人  农村大地主

//根据路径获得相册内的图片  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对象 。

上一篇下一篇

猜你喜欢

热点阅读