ios - 将图像解析到 ios 中的 Collection-view Custom 单元格
<p><p>我想解析来自网络服务的图像并显示到 Collection View 自定义单元格上,为此我编写了一个代码</p>
<p>在我的 .h 文件中</p>
<pre><code>@property(strong,nonatomic)IBOutlet UICollectionView *imagecollection;
@property(strong,nonatomic)NSArray *imagesa;
@property(strong,nonatomic)NSDictionary *json;
@property(strong,nonatomic)NSArray *aimages;
</code></pre>
<p>在我的 .m 文件中</p>
<pre><code>#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define imageURL
- (void)viewDidLoad
{
;
dispatch_async(kBgQueue, ^{
data = ;
;
});
forCellWithReuseIdentifier:@"CellIdentifier"];
}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = ;
self.imagesa=;
NSLog(@"images,%@",self.imagesa);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imagesa.count;
}
-(Custumcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Custumcell *cell=;
UIImageView *img=[initWithFrame:CGRectMake(0,0,100,100)];
NSString *img2=;
img.image=;
cell.imageview.image=;
return cell;
}
</code></pre>
<p>然后解析来自网络服务的图像,但未显示在 Collection View 中,请给我任何解决方案。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试替换你的</p>
<pre><code>-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = ;
self.imagesa=;
NSLog(@"images,%@",self.imagesa);
}
</code></pre>
<p>用代码</p>
<pre><code>-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = ;
self.imagesa=;
if (self.imagesa.count) {
dispatch_async(dispatch_get_main_queue(), ^{
;
});
}
NSLog(@"images,%@",self.imagesa);
}
</code></pre>
<p>现在使用 <a href="https://github.com/rs/SDWebImage" rel="noreferrer noopener nofollow">SDWebImageDownloader</a>并在您的 <code>cellForRowAtIndexpath</code> 方法中,将您的方法 cellForRowAtIndexPath 替换为 </p>
<pre><code>Custumcell *cell=;
NSDictionary *dict = ;
NSString *img2=;
] placeholderImage: options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"downloaded");
});
}];
return cell;
</code></pre>
<p>在你的文件中也导入 <code>#import "UIImageView+WebCache.h"</code></p>
<p>也许这会对你有所帮助。</p></p>
<p style="font-size: 20px;">关于ios - 将图像解析到 ios 中的 Collection-view Custom 单元格,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26353187/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26353187/
</a>
</p>
页:
[1]