Web サービスから解析された画像を表示しようとしていますが、画像が表示されません。ビューが読み込まれた後、画像が配列に追加されていることに気付きました。では、コレクション ビューに表示する画像を取得するにはどうすればよいでしょうか。いっそのこと、画像が読み込まれるように、メイン スレッドから実行する必要があるコード行はどれですか? ご協力ありがとうございました。
#import "ShowImagesCollectionViewController.h"
#import "ImageCell.h"
@interface ShowImagesCollectionViewController (){
NSMutableArray *array;
}
@property(nonatomic, strong)NSURLConnection *connection;
@property(nonatomic, strong)NSMutableData *webdata;
@end
@implementation ShowImagesCollectionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self getData];
array = [[NSMutableArray alloc]initWith];
[_collectionView reloadData];
}
-(void)getData
{
[_activyIndicator startAnimating];
NSURL *url = [NSURL URLWithString:@"WEBSITE"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
_connection = [NSURLConnection connectionWithRequest:requestURL delegate:self];
if (_connection) {
_webdata = [[NSMutableData alloc]init];
}
}
繋がり
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_webdata setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_webdata appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Fail With error");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[_activyIndicator startAnimating];
NSDictionary *allDataDictionary =[NSJSONSerialization JSONObjectWithData:_webdata options:kNilOptions error:nil];
NSDictionary *currentShowDictionary = [allDataDictionary objectForKey:@"Current_Show"];
NSArray *photoArray =[currentShowDictionary objectForKey:@"Photos"];
for (NSDictionary *diction in photoArray) {
NSDictionary *photo_urlDictionary = [diction objectForKey:@"photo_url"];
// NSLog(@"%@",photo_urlDictionary);
NSURL *collectionURL = [NSURL URLWithString:[photo_urlDictionary objectForKey:@"url"]];
NSData *colectionImageData = [NSData dataWithContentsOfURL:collectionURL];
UIImage *collectionImage = [UIImage imageWithData:colectionImageData];
[array addObject:collectionImage];
NSLog(@"%@",array);
}
[_activyIndicator stopAnimating];
}
コレクションビュー
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [array count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureCell" forIndexPath:indexPath];
NSString *myImageString = [array objectAtIndex:indexPath.row];
cell.pictureImageView.image = [UIImage imageNamed:myImageString];
return cell;
}