美文网首页
iOS根据URL获得手机中的图片

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

作者: 农村大地主 | 来源:发表于2018-02-08 09:35 被阅读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对象 。

    相关文章

      网友评论

          本文标题:iOS根据URL获得手机中的图片

          本文链接:https://www.haomeiwen.com/subject/yaizzxtx.html