Iphone 一个UICollectionViewCell中的UIImageView数量不定
我需要在一个UICollectionViewCell中显示几个图像。在获取远程照片信息数据之前,我不知道需要创建多少UIImageView。因此,我创建了一个如下所示的单元格: 它可以在此单元格中显示一个或多个图像。但当我滚动UICollectionViewController视图时,有时图像显示正确,有时不正确,有时一个图像覆盖另一个图像。告诉我我的代码出了什么问题?谢谢大家!Iphone 一个UICollectionViewCell中的UIImageView数量不定,iphone,ios,objective-c,uicollectionviewcell,Iphone,Ios,Objective C,Uicollectionviewcell,我需要在一个UICollectionViewCell中显示几个图像。在获取远程照片信息数据之前,我不知道需要创建多少UIImageView。因此,我创建了一个如下所示的单元格: 它可以在此单元格中显示一个或多个图像。但当我滚动UICollectionViewController视图时,有时图像显示正确,有时不正确,有时一个图像覆盖另一个图像。告诉我我的代码出了什么问题?谢谢大家! #import "ImageViewCell.h" #import "ImageViewWithPhotoInfo.
#import "ImageViewCell.h"
#import "ImageViewWithPhotoInfo.h"
#import "UIImageView+WebCache.h"
#import <QuartzCore/QuartzCore.h>
#import "GGFullScreenImageViewController.h"
#define ONE_CELL_IMAGE_PADDING 2.f
#define GIF_IMAGE_WIDTH 50.f
#define IMAGEVIEW_BASE_TAG 100
#define GIFVIEW_BASE_TAG 1000
#define HALF_SCREEN ([[UIScreen mainScreen] bounds].size.width-5.f)/2
@interface ImageViewCell(){
NSUInteger _photoCount;
UICollectionViewController *parentController;
}
@end
@implementation ImageViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void) setNeededPhotoInfo:(NSDictionary *)photoInfo parentController:(UICollectionViewController*)controller atIndexPath:(NSIndexPath*)indexPath{
currentIndexPath = indexPath;
parentController = controller;
self.photoInfo = photoInfo;
NSArray *photos = [photoInfo objectForKey:@"photos"];
NSUInteger count = photos.count;
if (photos && photos.count) {
_photoCount = count;
CGFloat imageViewWidth = HALF_SCREEN - ONE_CELL_IMAGE_PADDING;
CGFloat currentHeight = 0;
int i = 0;
ImageViewWithPhotoInfo *asyncimageView;
for (NSDictionary *singlePhotoInfo in photos) {
asyncimageView = [[ImageViewWithPhotoInfo alloc] init];
asyncimageView.photoInfo = self.photoInfo;
asyncimageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClicked:)];
singleTap.numberOfTapsRequired = 1;
asyncimageView.userInteractionEnabled = YES;
asyncimageView.photoIndex = i;
asyncimageView.tag = IMAGEVIEW_BASE_TAG + i;
[asyncimageView addGestureRecognizer:singleTap];
NSArray *altPhotos = [singlePhotoInfo objectForKey:@"alt_sizes"];
int count = altPhotos.count;
if (altPhotos && count) {
int fetchLocaltion;
if (count > 2) {
fetchLocaltion = 2;
}else{
fetchLocaltion = count - 1;
}
NSDictionary *altPhoto = [altPhotos objectAtIndex:fetchLocaltion];
if (altPhoto && altPhoto.count) {
CGFloat thisHeight = [[altPhoto objectForKey:@"height"] floatValue];
CGFloat thisWidth = [[altPhoto objectForKey:@"width"] floatValue];
CGFloat rate = imageViewWidth / thisWidth;
CGFloat shouldHeight = thisHeight * rate;
asyncimageView.frame = CGRectMake( 0, ONE_CELL_IMAGE_PADDING + i * ONE_CELL_IMAGE_PADDING + currentHeight, imageViewWidth, shouldHeight);
NSLog(@"currentheight === %f",currentHeight);
[self.contentView addSubview:asyncimageView];
currentHeight += shouldHeight;
}
NSString *url = [altPhoto objectForKey:@"url"];
// [asyncimageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"black_image_default.png"]];
__weak ImageViewWithPhotoInfo *blockAsyncImageView = asyncimageView;
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:url];
if (cachedImage) {
asyncimageView.image = cachedImage;
}else{
asyncimageView.image = [UIImage imageNamed:@"black_image_default.png"];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:url] options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (currentIndexPath == indexPath) {
blockAsyncImageView.image = image;
}else{
blockAsyncImageView.image = nil;
}
[[SDImageCache sharedImageCache] storeImage:image forKey:url toDisk:NO];
}];
}
if ([url hasSuffix:@".gif"]){
UIImageView *gifView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gificon.png"]];
CGSize asyncImageViewSize = asyncimageView.bounds.size;
gifView.frame = CGRectMake((asyncImageViewSize.width - GIF_IMAGE_WIDTH) / 2, (asyncImageViewSize.height - GIF_IMAGE_WIDTH) / 2 + asyncimageView.frame.origin.y, GIF_IMAGE_WIDTH, GIF_IMAGE_WIDTH);
[self.contentView addSubview:gifView];
}
}
altPhotos = nil;
i++;
asyncimageView = nil;
}
}
photos = nil;
}
-(void)imageViewClicked:(UIGestureRecognizer *)sender{
ImageViewWithPhotoInfo *imageView = (ImageViewWithPhotoInfo*)sender.view;
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
vc.photoIndex = imageView.photoIndex;
vc.userInfo = imageView.photoInfo;
[parentController presentViewController:vc animated:YES completion:nil];
}
#导入“ImageViewCell.h”
#导入“ImageViewWithPhotoInfo.h”
#导入“UIImageView+WebCache.h”
#进口
#导入“GGFullScreenImageViewController.h”
#定义一个单元格\u图像\u填充2.f
#定义GIF\u图像\u宽度50.f
#定义IMAGEVIEW_BASE_标签100
#定义GIF视图\基础\标记1000
#定义半屏幕([[UIScreen mainScreen]边界].size.width-5.f)/2
@接口ImageViewCell(){
NSU整数光计数;
UICollectionViewController*父控制器;
}
@结束
@ImageViewCell的实现
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
}
回归自我;
}
-(void)SetNeedPhotoInfo:(NSDictionary*)photoInfo父控制器:(UICollectionViewController*)控制器atIndexPath:(NSIndexPath*)indexPath{
currentIndexPath=indexPath;
父控制器=控制器;
self.photoInfo=photoInfo;
NSArray*照片=[photoInfo objectForKey:@“照片”];
NSU整数计数=photos.count;
如果(照片和照片计数){
_光电计数=计数;
CGFloat IMAGEVIEWWITH=半屏幕-一个单元格\u图像\u填充;
CGFloat currentHeight=0;
int i=0;
带有PhotoInfo*异步图像视图的图像视图;
用于(NSDictionary*照片中的singlePhotoInfo){
asyncimageView=[[ImageViewWithPhotoInfo alloc]init];
asyncimageView.photoInfo=self.photoInfo;
asyncimageView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UITapGestureRecognizer*singleTap=[[UITapGestureRecognizer alloc]initWithTarget:自操作:@selector(imageViewClicked:)];
singleTap.numberOfTapsRequired=1;
asyncimageView.userInteractionEnabled=是;
asyncimageView.photoIndex=i;
asyncimageView.tag=IMAGEVIEW\u BASE\u tag+i;
[asyncimageView AddGestureRecognitor:singleTap];
NSArray*altPhotos=[singlePhotoInfo objectForKey:@“alt_尺寸”];
int count=altPhotos.count;
如果(备用照片和计数){
int-fetchlocalation;
如果(计数>2){
fetchLocation=2;
}否则{
fetchLocation=count-1;
}
NSDictionary*altPhoto=[altPhotos objectAtIndex:fetchLocation];
if(altPhoto&&altPhoto.count){
CGFloat thisHeight=[[altPhoto objectForKey:@“height”]floatValue];
CGFloat thisWidth=[[altPhoto objectForKey:@“width”]floatValue];
CGFloat rate=imageViewWidth/thisWidth;
CGFloat shouldHeight=此高度*速率;
asyncimageView.frame=CGRectMake(0,一个单元格图像填充+i*一个单元格图像填充+currentHeight,imageViewWidth,shouldHeight);
NSLog(@“currentheight===%f”,currentheight);
[self.contentView addSubview:asyncimageView];
当前高度+=应高度;
}
NSString*url=[altPhoto objectForKey:@“url”];
//[asyncimageView setImageWithURL:[NSURL URLWithString:url]占位符图像:[UIImage ImageName:@“black_image_default.png”];
__弱ImageViewWithPhotoInfo*blockAsyncImageView=asyncimageView;
UIImage*cachedImage=[[SDImageCache sharedImageCache]imageFromMemoryCacheForKey:url];
if(缓存图像){
asyncimageView.image=cachedImage;
}否则{
asyncimageView.image=[UIImage ImageName:@“black_image_default.png”];
[[SDWebImageDownloader sharedDownloader]DownloadeImageWithURL:[NSURL URLWithString:url]选项:0进度:^(NSUInteger receivedSize,long-long-expectedSize){
}已完成:^(UIImage*图像,NSData*数据,NSError*错误,布尔已完成){
if(currentIndexPath==indexPath){
blockAsyncImageView.image=图像;
}否则{
blockAsyncImageView.image=nil;
}
[[SDImageCache sharedImageCache]storeImage:image-forKey:url-toDisk:NO];
}];
}
如果([url hasSuffix:@.gif”]){
UIImageView*gifView=[[UIImageView alloc]initWithImage:[UIImageName:@“gificon.png”];
CGSize asyncImageViewSize=asyncimageView.bounds.size;
gifView.frame=CGRectMake((asyncImageViewSize.width-GIF\u IMAGE\u width)/2,(asyncImageViewSize.height-GIF\u IMAGE\u width)/2+asyncimageView.frame.origin.y,GIF\u IMAGE\u width,GIF\u IMAGE\u width);
[self.contentView addSubview:gifView];
}
}
altPhotos=零;
i++;
asyncimageView=nil;
}
}
照片=无;
}
-(无效)imageViewClicked:(UIgestureRecognitor*)发件人{
ImageViewWithPhotoInfo*imageView=(ImageViewWithPhotoInfo*)sender.view;
GGFullscreenImageViewController*vc=[[GGFullscreenImageViewController alloc]init];
vc.liftedImageView=imageView;
vc.photoIndex=imageView.photoIndex;
vc.userInfo=imageView.photoInfo;
[parentController presentViewController:vc动画:是完成:无];
}
查看AFNetworking library,它对于进行网络呼叫非常有用。AFNetworking还提供了
[imageView setImageWithURL:[NSURL URLWithString:@"…"]];