Ios 如何将UIImage数组添加到集合视图
提前感谢您查看此项目并帮助我。Ios 如何将UIImage数组添加到集合视图,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,提前感谢您查看此项目并帮助我。 故事板连接 我正在尝试在“收藏”视图中显示UIImage数组。我已经连接了故事板中的所有内容 我的ViewController是集合视图的委托和数据源 “我的单元格”包含一个UIImageView,该视图在自定义单元格的头文件中有一个IBOutlet。 应用程序当前的工作方式 该应用程序基于 由用户进行搜索 用户输入搜索后,ViewController将通过 NSString被转移到模型(GetImageURL.h和GetImageURL.m.) 这似乎是基于我正
故事板连接 我正在尝试在“收藏”视图中显示UIImage数组。我已经连接了故事板中的所有内容
我的ViewController是集合视图的委托和数据源
“我的单元格”包含一个UIImageView,该视图在自定义单元格的头文件中有一个IBOutlet。
应用程序当前的工作方式
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
*** First throw call stack:
(0x1c97012 0x10d4e7e 0x1c96deb 0x518748 0x533b1b 0x533646 0xf9ff8 0xfa232 0x5338d5 0x29a4 0x3ef6 0xb13fb4 0xb13e67 0x30ca 0x49fb53f 0x4a0d014 0x49fd7d5 0x1c3daf5 0x1c3cf44 0x1c3ce1b 0x1bf17e3 0x1bf1668 0x18ffc 0x1b9d 0x1ac5)
libc++abi.dylib: terminate called throwing an exception
我的问题 我想知道我在创建collectionView时做错了什么,导致它在稍后重新加载数据时崩溃。
代码 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UICollectionViewController <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIApplicationDelegate>
//receives a mutable array of UIImage(s)
-(void)receiveArrayOfImages: (NSMutableArray *)arrayOfImages;
@end
GetImageURL.h
#import <Foundation/Foundation.h>
#import "ViewController.h"
@interface GetImageURL : NSObject
@property (strong, nonatomic) NSString *bookName;
@property (strong, nonatomic) NSMutableArray *imageArray;
-(void)recieveBookUrl:(NSString *)bookName; //recieves a string from the viewController that the user inputs
@end
很抱歉将所有代码都放在这里。我想如果一切都在那里会容易些。也许这对其他人也有帮助
谢谢 您的布局是否设置为流动?在故事板中应该是这样的:
如果将其设置为“自定义”,则需要自己创建布局。如果未在故事板中创建集合视图单元,则我认为您必须注册它 如果你的手机被编程了
// Add this in your controller's viewDidLoad method
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellID];
如果您的单元格有一个nib文件,我认为它是registerNB:forCellWithReuseIdentifier:我没有更改任何设置,只是为我的单元格分配了正确的类并更改了它的reuseID。我感谢你的帮助!谢谢你的回复。虽然我同意cellForItemAtIndexPath:可能是我的问题,但您的代码有点问题。我在您创建UIImage*小图像以及下面的行时遇到错误。另外,如果我的数组已经包含UIImage,是否需要创建另一个UIImage?再次感谢你的帮助!现在,请尝试此代码并检查是否已将序列图像板中collectionviewcell内的imageview标记设置为100。此外,数组[indexpath]使用您的数组名称。我已在情节提要中将单元格的标记设置为100,并将“数组[indexpath]替换为self.arrayOfImages。它应该是self.arrayOfImages[indexpath]?我收到一个错误,指出UICollectionView必须使用非nil布局参数初始化。您需要在序列图像板上为集合视图控制器设置标识符,否则它不会返回任何内容在集合视图控制器的标识检查器中,我将序列图像板ID和还原ID设置为collectionViewCOnController。我需要在我的代码中的某个地方使用它吗?谢谢你的评论。我正在使用我在某个地方读过并尝试过的原型单元格,它包含在故事板中的集合视图控制器中。我是否最好以编程方式制作该单元格?我在苹果的文档中找到了registerClass:,但它没有帮助所以我把它拿出来了。还有其他想法吗?再次感谢你的帮助。
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UIImage *smallimage = [UIImage imageNamed:[NSString StringWithFormat:@"%@.png"[array[indexpath]]];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *gridimageview = (UIImageView *)[cell viewWithTag:100];
[gridimageview setImage:smallimage];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"index path is %d", indexPath.row);
//Enter the action you want when an image is clicked in collection view
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
// Write your code here to deselect the selected image
}
}
#import <Foundation/Foundation.h>
#import "ViewController.h"
@interface GetImageURL : NSObject
@property (strong, nonatomic) NSString *bookName;
@property (strong, nonatomic) NSMutableArray *imageArray;
-(void)recieveBookUrl:(NSString *)bookName; //recieves a string from the viewController that the user inputs
@end
#import "GetImageURL.h"
@interface GetImageURL()
@property (strong, nonatomic) NSString *pageID;
@property (strong, nonatomic) NSMutableArray *imageUrlArray;
@end
@implementation GetImageURL
//takes the string that the user entered and alters it to fit the wikipedia API format
-(void)recieveBookUrl:(NSString *)bookName {
self.imageUrlArray = [[NSMutableArray alloc] init];
self.imageArray = [[NSMutableArray alloc] init];
bookName = [bookName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //or use string by replacing @" " with @"%20";
NSString *wikiAPI = @"http://en.wikipedia.org/w/api.php?format=json&indexpageids&action=query&prop=images&titles=";
bookName = [wikiAPI stringByAppendingString:bookName];
NSURL *bookURL = [NSURL URLWithString:bookName];
//Plan to put this action in the background later
dispatch_async(dispatch_get_main_queue(), ^() {
NSData *data = [NSData dataWithContentsOfURL:bookURL];
[self performSelectorOnMainThread:@selector(getBookTitleApi:) withObject:data waitUntilDone:YES];
});
}
//first part of the API sucessfully gets the padeID and all the images returned by the API.
-(void)getBookTitleApi: (NSData *)data {
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSDictionary *parseQueryBookTitle = [json objectForKey:@"query"];
NSArray *parsePageIdsBookTitle = [parseQueryBookTitle objectForKey:@"pageids"];
for (NSDictionary *pageIdBookTitle in parsePageIdsBookTitle) {
self.pageID = [NSString stringWithFormat:@"%@", pageIdBookTitle];
}
NSDictionary *parsePagesBookTitle = [parseQueryBookTitle objectForKey:@"pages"];
NSDictionary *parsePageIdBookTitle = [parsePagesBookTitle objectForKey:self.pageID];
NSDictionary *parseImagesBookTitle = [parsePageIdBookTitle objectForKey:@"images"];
NSDictionary *valueTitleBookTitle = [parseImagesBookTitle valueForKey:@"title"];
for (NSDictionary *imageFilenames in valueTitleBookTitle) {
NSString *imageFilename = [NSString stringWithFormat:@"%@", imageFilenames];
imageFilename = [imageFilename substringFromIndex:5];
imageFilename = [imageFilename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //or use string by replacing @" " with @"%20";
NSString *wikiImageAPI = @"http://en.wikipedia.org/w/api.php?action=query&format=json&indexpageids&prop=imageinfo&iiprop=url&titles=Image:";
imageFilename = [wikiImageAPI stringByAppendingString:imageFilename];
NSURL *imageFilenameUrl = [NSURL URLWithString:imageFilename];
NSData *data = [NSData dataWithContentsOfURL:imageFilenameUrl];
[self performSelectorOnMainThread:@selector(getImageApi:) withObject:data waitUntilDone:YES];
}
//Uses the URLs to create an NSData object and then uses that data to create a UIImage which is added to a Mutable Array
for (NSString *jpgURL in self.imageUrlArray) {
NSURL *imageURL = [NSURL URLWithString:jpgURL];
NSLog(@"This is an imageURL in GetImageURL.m: %@", imageURL);
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
[self.imageArray addObject:image];
}
NSLog(@"The array of images in GetImageURL.m contains %d images called %@", [self.imageArray count], self.imageArray);
ViewController *view = [[ViewController alloc] init];
[view receiveArrayOfImages:self.imageArray]; //sends the array of images to the View Controller
//objects removed from array so that next user search can start over
[self.imageArray removeAllObjects];
[self.imageUrlArray removeAllObjects];
}
//Uses the image filenames from the first section and gets the specific URLS for the image. Limits results to .jpg images
-(void)getImageApi: (NSData *)data {
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *parseQueryImage = [json objectForKey:@"query"];
NSDictionary *parsePageIdsImage = [parseQueryImage objectForKey:@"pageids"];
for (NSString *pageIdImage in parsePageIdsImage) {
NSDictionary *parsePagesImage = [parseQueryImage objectForKey:@"pages"];
NSDictionary *parsePageIdImage = [parsePagesImage objectForKey:pageIdImage];
NSDictionary *parseImageInfoImage = [parsePageIdImage objectForKey:@"imageinfo"];
NSDictionary *valueImageInfoUrlsImage = [parseImageInfoImage valueForKey:@"url"];
for (NSString *valueImageInfoUrlImage in valueImageInfoUrlsImage) {
if ([valueImageInfoUrlImage hasSuffix:@".jpg"]) {
[self.imageUrlArray addObject:valueImageInfoUrlImage];
}
}
}
}
@end
// Add this in your controller's viewDidLoad method
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellID];
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UIImage *smallimage = [UIImage imageNamed:[NSString StringWithFormat:@"%@.png"[array[indexpath]]];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *gridimageview = (UIImageView *)[cell viewWithTag:100];
[gridimageview setImage:smallimage];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"index path is %d", indexPath.row);
//Enter the action you want when an image is clicked in collection view
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
// Write your code here to deselect the selected image
}
}