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
感謝您的幫助 –