iOS-显示来自“ALAssetLibrary”的图像`
我有iOS-显示来自“ALAssetLibrary”的图像`,ios,objective-c,iphone,Ios,Objective C,Iphone,我有ViewController1和ViewController2。 ViewController1是UICollectionView,它使用ASAssetLibrary将缩略图加载到UICollectionViewCell: NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0]; ALAssetsLibrary *al = [TemplateEditBarController defaultAssetsL
ViewController1
和ViewController2
。
ViewController1
是UICollectionView
,它使用ASAssetLibrary
将缩略图加载到UICollectionViewCell
:
NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
ALAssetsLibrary *al = [TemplateEditBarController defaultAssetsLibrary];
[al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset) {
[collector addObject:asset];
}
}];
assetsarray = collector;//<--assetsarray is local.
}
failureBlock:^(NSError *error) { NSLog(@"fail!");}
];
我得到:
资产-library://asset/asset.JPG?id=6E5438ED-9A8C-4ED0-9DEA-AB2D8F8A9360&ext=JPG
我尝试更改为[url路径]
我得到:
asset.JPG
如何获取实际的照片url,以便我可以转换为
NSString
以传递到ViewController2
?显然除了传递ALAsset
之外,没有任何直接的解决方案。我就是这么做的:
NSURL* aURL = [NSURL URLWithString:imagePath];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:aURL resultBlock:^(ALAsset *asset)
{
UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
imageView.image=image;
}
failureBlock:^(NSError *error)
{
// error handling
NSLog(@"failure-----");
}];
在ViewController1
中,即UICollectionViewController
,我具有以下功能:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(@"selected row: %ld",(long)indexPath.row);
long row = [indexPath row];
NSString* newbg = [[NSString alloc]init];
if ([bartype isEqualToString:@"photolibrary"]) {
ALAsset* asset = backgroundsarray[row];
[_delegate changeBackgroundWithAsset:asset];
}
然后我在ViewController2
中有一个函数来处理ALAsset
:
-(void)changeBackgroundWithAsset:(ALAsset*)b{
ALAssetRepresentation *rep = [b defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *bimage;
bimage = [UIImage imageWithCGImage:iref];
UIImageView* bgview = [[UIImageView alloc]init];
bgview = curbgview;
bgview.image = bimage;
}我的解决方案如下:
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
if (asset) {
image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
}
}
查看URL,但我不知道为什么加载的图像的分辨率不是完整的。
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
if (asset) {
image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
}
}