我自己在我的應用這個功能現在是一個天。我不能使用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];
此代碼將使這樣的佈局。
你的問題現在第二部分。如何顯示全屏圖像,縮放功能和向左/向右滑動以顯示下一個以前的圖像。 這實際上很容易實現。我使用第三方庫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;
}
攻第三圖像會顯示在全屏幕,左/右箭頭或刷卡左/右功能。此外還有縮放縮放和平移縮放照片。
希望這可以幫助你。
感謝您的詳細回答,但它更我感興趣的細節。 例如,在這張專輯中選擇圖像時,動畫很好地進入全屏圖像,同時逐步從改變將正方形圖像轉換爲原始高寬比。 裁剪小(方形)圖像以便圖像不會擠在一起,並且它被內插,因此圖像中看不到鋸齒狀邊緣。這些圖像平滑過渡到全屏圖像。我會編輯我的問題,以更具體 –