我自己现在正在我的应用程序中制作这个功能。我不能使用UICollectionView
,因为它仅在 >=iOS6 中可用。我手动创建了一个网格布局,实际上它很容易实现。
在 .h 文件中放这个
{
int thumbWidth;
int thumbHeight;
int photosInRow;
int xPos;
int yPos;
int padding;
int photosCount;
}
@property (strong, nonatomic) UIScrollView *photoScroll;
@property(nonatomic, strong) NSArray *photosArray;
在 .m 文件中
thumbHeight = 73; // 2px is reduced for border
thumbWidth = 73; // 2px is reduced for border
photosInRow = 4;
padding = 4;
xPos = 0;
yPos = 0;
您可以根据需要更改上述值。然后填充图像并动态创建网格布局,如下所示:
photoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(padding, padding, self.view.frame.size.width, self.view.frame.size.height)];
photoScroll.showsVerticalScrollIndicator = NO;
photoScroll.showsHorizontalScrollIndicator = NO;
[photoScroll setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
photosCount = [photosArray count];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
for (int counter = 0; counter < photosCount; counter++) {
// Set x, y positions
if (counter % photosInRow == 0 && counter !=0) {
yPos = yPos+thumbHeight+padding;
xPos = 0;
} else {
if (counter != 0) {
xPos = xPos+thumbWidth+padding;
}
}
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
[[btnImage layer] setBorderWidth:1.0f];
[[btnImage layer] setBorderColor:[UIColor whiteColor].CGColor];
DBPhotos *photo = (DBPhotos *)[photosArray objectAtIndex:counter];
NSURL *url = [NSURL URLWithString: photo.thumb];
[manager downloadWithURL:url delegate:self options:0
success:^(UIImage *image){
[btnImage setBackgroundImage:image forState:UIControlStateNormal];
}
failure:^(NSError *error) {
NSLog(@"Error Download image %@", [error localizedDescription]);
}];
[btnImage setFrame:CGRectMake(xPos, yPos, thumbWidth, thumbHeight)];
[btnImage setTag:counter];
[btnImage addTarget:self action:@selector(showFullImage:) forControlEvents:UIControlEventTouchUpInside];
[photoScroll addSubview:btnImage];
}
[photoScroll setContentSize:CGSizeMake(self.view.frame.size.width, (ceil(photosCount/photosInRow)*(thumbHeight+padding))+thumbHeight+64)];
[self.view addSubview:photoScroll];
你会看到我正在使用UIButton
而不是UIImage
,这是为了如果用户点击图像,我可以在这一行添加 get 和事件处理程序来显示全屏图像:
[btnImage addTarget:self action:@selector(showFullImage:) forControlEvents:UIControlEventTouchUpInside];
此代码将制作这样的布局。
现在是你问题的第二部分。如何显示全屏图像,具有捏缩放功能和向左/向右滑动以显示下一个以前的图像。这实际上很容易实现。我正在使用 3rd 方库MWPhotoBrowser
只需下载它,就可以很容易地制作像 UI 一样的照片库。这是我在showFullImage
功能上所做的。
- (void)showFullImage:(UIButton*)sender{
[self.browser setInitialPageIndex:sender.tag];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.browser];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:nc animated:YES completion:nil];
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return self.photosArray.count;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < self.photosArray.count) {
DBPhotos *photo = (DBPhotos *) [self.photosArray objectAtIndex:index];
return [MWPhoto photoWithURL:[NSURL URLWithString:photo.image]];
}
return nil;
}
点击第三张图片将全屏显示,带有向左/向右箭头或向左/向右滑动功能。还带有捏缩放和平移缩放照片。
希望这对您有所帮助。