我需要在一个 UICollectionViewCell 中显示几张图片。在获得远程照片信息数据之前,我不知道需要创建多少 UIImageView。所以我创建了一个如下所示的单元格:
它可能会在此单元格中显示一个或多个图像。但是当我滚动 UICollectionViewController 视图时,有时图像显示正确,有时不正确,有时一个图像覆盖另一个图像。告诉我我的代码有什么问题?谢谢!
#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];
}