gpt4 book ai didi

ios - 从解析中下载图像并在 UICollectionView 中显示

转载 作者:行者123 更新时间:2023-11-28 21:26:46 25 4
gpt4 key购买 nike

我正在从解析中下载图像,之后我在 uicollection View 中显示该图像,但是当我滚动 Collection View 时它挂起。这是我的代码

-(void)viewWillAppear:(BOOL)animated
{
CGRect screen = [[UIScreen mainScreen] bounds];

CGFloat height = CGRectGetHeight(screen);

PFQuery * query = [PFQuery queryWithClassName:@"Static_Wallpaper"];
[query selectKeys:@[@"thumbnail"]];
if (height == 480) {
[query selectKeys:@[@"image4s"]];
}
else{
[query selectKeys:@[@"image6s"]];
}

[query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError * error){
if (!error) {
[imgArry arrayByAddingObjectsFromArray:objects];
imgArry = [[NSMutableArray alloc] initWithArray:objects];
NSLog(@"%lu",(unsigned long)imgArry.count);
[cllectionView reloadData];

}

}];

}

这是在 Collection View 中填充图像的代码

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return imgArry.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
PFObject *imageObject = [imgArry objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:@"image4s"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error){
NSLog(@"is there any data? %@", data);
cell.imgView.image = [UIImage imageWithData:data];
;

}
else {
NSLog(@"no data!");
}
}];



return cell;
}

最佳答案

通过添加 SDWebimage您需要执行如下代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
PFObject *imageObject = [imgArry objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:@"image4s"];
NSString *UserProfile = [file url];

if([[SDWebImageManager sharedManager] diskImageExistsForURL:[NSURL URLWithString:UserProfile]])
{
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:UserProfile]];
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
cell.imgView.image=image;
}
else
{
cell.imgView.image=[UIImage imageNamed:@"UserUpdationImageCell.png"];
[cell.imgView sd_setImageWithURL:[NSURL URLWithString:UserProfile] placeholderImage:[UIImage imageNamed:@"UserUpdationImageCell.png"]];
}



return cell;
}

关于ios - 从解析中下载图像并在 UICollectionView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37829881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com