• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将图像解析到 ios 中的 Collection-view Custom 单元格

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:30:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想解析来自网络服务的图像并显示到 Collection View 自定义单元格上,为此我编写了一个代码

在我的 .h 文件中

@property(strong,nonatomic)IBOutlet UICollectionView *imagecollection;
@property(strong,nonatomic)NSArray *imagesa;
@property(strong,nonatomic)NSDictionary *json;
@property(strong,nonatomic)NSArray *aimages;

在我的 .m 文件中

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)


#define imageURL [NSURL URLWithString"http://www.truemanindiamagazine.com/webservice/gallery_image.php"]

- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: imageURL];
    [self performSelectorOnMainThreadselector(fetchedData withObject:data waitUntilDone:YES];
});

[self.imagecollection registerNib:[UINib nibWithNibName"Custumcell" bundle:nil] forCellWithReuseIdentifier"CellIdentifier"];
}
-(void)fetchedDataNSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey"data"];
NSLog(@"images,%@",self.imagesa);
}

 -(NSInteger)numberOfSectionsInCollectionViewUICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section
{
return self.imagesa.count;
}
-(Custumcell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
 {
Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier"CellIdentifier" forIndexPath:indexPath];
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
NSString *img2=[self.imagesa objectAtIndex:indexPath.row];
img.image=[UIImage imageNamed:img2];
cell.imageview.image=[UIImage imageNamed:img2];
return cell;
}

然后解析来自网络服务的图像,但未显示在 Collection View 中,请给我任何解决方案。



Best Answer-推荐答案


尝试替换你的

-(void)fetchedDataNSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey"data"];
NSLog(@"images,%@",self.imagesa);
}

用代码

-(void)fetchedDataNSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey"data"];
if (self.imagesa.count) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [imagecollection reloadData];
            });
      }
NSLog(@"images,%@",self.imagesa);
}

现在使用 SDWebImageDownloader并在您的 cellForRowAtIndexpath 方法中,将您的方法 cellForRowAtIndexPath 替换为

Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier"CellIdentifier" forIndexPath:indexPath];
    NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];
    NSString *img2=[dict valueForKey"link"];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"temp.png"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"downloaded");
        });
    }];
    return cell;

在你的文件中也导入 #import "UIImageView+WebCache.h"

也许这会对你有所帮助。

关于ios - 将图像解析到 ios 中的 Collection-view Custom 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26353187/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap