OStack程序员社区-中国程序员成长平台

标题: ios - 将图像解析到 ios 中的 Collection-view Custom 单元格 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:30
标题: ios - 将图像解析到 ios 中的 Collection-view Custom 单元格

我想解析来自网络服务的图像并显示到 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4