0
我试图通过PHP在集合视图中加载图像,如下面的代码所示;集合查看未显示图像
@interface SearchMainPost()
{
NSMutableArray *myObject;
// A dictionary object
NSDictionary *dict;
// Define keys
NSString *imageid;
NSString *name;
NSString *path;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Define keys
imageid = @"videoImage";
name = @"timeLineVideoUserName";
path = @"TheIndex";
// Create array to hold dictionaries
myObject = [[NSMutableArray alloc] init];
NSData *jsonData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://mywebSite/list/list.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
NSString *strImageID = [dataDict objectForKey:@"videoImage"];
NSString *strName = [dataDict objectForKey:@"timeLineVideoUserName"];
NSString *strPath = [dataDict objectForKey:@"TheIndex"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
strImageID, imageid,
strName, name,
strPath, path,
nil];
[myObject addObject:dict];
NSLog(@"%@", strImageID);
}
}
她是我如何实现收集;
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return myObject.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SearchMainPostCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:path]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
myCell.displayImage.image = img;
// myCell.displayDetail.text= [tmpDict objectForKey:name];
return myCell;
}
我的问题是图像没有显示在单元格中,所有的集合视图都是黑色的。 我可以看到控制台中显示的信息,我可以看到图像链接。集合视图单元已被设置并在属性检查器中将标识符设置为“myCell”。
我很抱歉我的解释,我希望有人会帮助我如何显示图像。
在此先感谢。
谢谢,这是工作。但只要图像加载和当我开始按任何图像,图像开始随机 –
使用NSCache保存图像和重用,否则你必须每次下载图像为什么图像随机变化。 – Jeyamahesan
感谢您的帮助 –