[英]Get Image from URL and display them to Image Gallery AlAsset
我知道這個問題可能會被問很多次,但是我是一個初學者,所以溫柔一點是一個謙虛的要求。
我正在嘗試制作一個圖像庫,以顯示URL的所有圖像。
我已經使用了所有資產框架,並制作了一個簡單的圖庫,可以返回相機膠卷的圖像,我想擁有一些URL的圖像,因此請幫助我從url獲取圖像並將其顯示在Image Gallery中
這是視圖控制器頭文件
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface ViewController : UIViewController <UICollectionViewDelegate,UICollectionViewDataSource>
@property (retain, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic, strong) NSArray *assets;
這是我已在其中注冊一個.NIB文件以將Cell添加到我的CollectionView
的實現文件
#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "MyCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];
_assets = [@[] mutableCopy];
__block NSMutableArray *tmpAssets = [@[] mutableCopy];
// 1
這是調用defaultAssetsLibrary
方法,稍后您將看到
ALAssetsLibrary *assetsLibrary = [ViewController defaultAssetsLibrary];
// 2
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result)
{
// 3
[tmpAssets addObject:result];
}
}];
// 4
//NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
//self.assets = [tmpAssets sortedArrayUsingDescriptors:@[sort]];
self.assets = tmpAssets;
// 5
[self.collectionView reloadData];
} failureBlock:^(NSError *error) {
NSLog(@"Error loading images %@", error);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - collection view data source
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.assets.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (MyCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
ALAsset *asset = self.assets[indexPath.row];
cell.asset = asset;
cell.backgroundColor = [UIColor redColor];
////
//=
cell.cellLabel.text = [NSString stringWithFormat:@"cell %i",indexPath.row];
return cell;
}
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
ALAsset *asset = self.assets[indexPath.row];
ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];
// Do something with the image
}
這是我認為返回照片庫的方法
+ (ALAssetsLibrary *)defaultAssetsLibrary
{
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
請幫助我從URL獲取圖像並將其顯示在圖像庫中。
就MyCell文件而言。
這是MYCell.m文件
@interface MyCell ()
// 1
@property(nonatomic, strong) IBOutlet UIImageView *photoImageView;
@end
@implementation MyCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) setAsset:(ALAsset *)asset
{
// 2
_asset = asset;
self.photoImageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
}
如果我們從URL獲取圖像,則實際上不需要AlAsset
作為圖像庫。
我已經按照本教程制作了圖像庫Guide,並且Simple通過了url數組,並且對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.