MWPhotoBrowser 圖片瀏覽功能使用
阿新 • • 發佈:2019-01-12
@weakify(self); photosCell.tapImgBlock = ^(NSInteger index) { @strongify(self); // Browser NSMutableArray *photos = [[NSMutableArray alloc] init]; NSMutableArray *thumbs = [[NSMutableArray alloc] init]; BOOL displayActionButton = YES; BOOL displaySelectionButtons = NO; BOOL displayNavArrows = NO; BOOL enableGrid = YES; BOOL startOnGrid = NO; BOOL autoPlayOnAppear = NO; // 將你的圖片url或者UIImage包裝成 MWPhoto 放到photos陣列中 /* your code; */ // 傳給self的photos陣列 self.photos = photos; self.thumbs = thumbs; // Create browser MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self]; browser.displayActionButton = displayActionButton; browser.displayNavArrows = displayNavArrows; browser.displaySelectionButtons = displaySelectionButtons; browser.alwaysShowControls = displaySelectionButtons; browser.zoomPhotosToFill = NO; browser.enableGrid = enableGrid; browser.startOnGrid = startOnGrid; browser.enableSwipeToDismiss = NO; browser.autoPlayOnAppear = autoPlayOnAppear; [browser setCurrentPhotoIndex:index]; // Push [self.navigationController pushViewController:browser animated:YES]; };
browser.zoomPhotosToFill = NO;
注意:browser的這個zoomPhotosToFill屬性設定為NO,否則的話,圖片瀏覽器中的圖片時會自動根據圖片和螢幕的寬高計算縮放比例,進而導致圖片顯示不全
相關程式碼:
MWZoomingScrollView
- (void)setMaxMinZoomScalesForCurrentBounds { // Reset self.maximumZoomScale = 1; self.minimumZoomScale = 1; self.zoomScale = 1; // Bail if no image if (_photoImageView.image == nil) return; // Reset position _photoImageView.frame = CGRectMake(0, 0, _photoImageView.frame.size.width, _photoImageView.frame.size.height); // Sizes CGSize boundsSize = self.bounds.size; CGSize imageSize = _photoImageView.image.size; // Calculate Min CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise CGFloat minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible // Calculate Max CGFloat maxScale = 3; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // Let them go a bit bigger on a bigger screen! maxScale = 4; } // Image is smaller than screen so no zooming! if (xScale >= 1 && yScale >= 1) { minScale = 1.0; } // Set min/max zoom self.maximumZoomScale = maxScale; self.minimumZoomScale = minScale; // Initial zoom self.zoomScale = [self initialZoomScaleWithMinScale]; // If we're zooming to fill then centralise if (self.zoomScale != minScale) { // Centralise self.contentOffset = CGPointMake((imageSize.width * self.zoomScale - boundsSize.width) / 2.0, (imageSize.height * self.zoomScale - boundsSize.height) / 2.0); } // Disable scrolling initially until the first pinch to fix issues with swiping on an initally zoomed in photo self.scrollEnabled = NO; // If it's a video then disable zooming if ([self displayingVideo]) { self.maximumZoomScale = self.zoomScale; self.minimumZoomScale = self.zoomScale; } // Layout [self setNeedsLayout]; }
- (CGFloat)initialZoomScaleWithMinScale { CGFloat zoomScale = self.minimumZoomScale; if (_photoImageView && _photoBrowser.zoomPhotosToFill) { // Zoom image to fill if the aspect ratios are fairly similar CGSize boundsSize = self.bounds.size; CGSize imageSize = _photoImageView.image.size; CGFloat boundsAR = boundsSize.width / boundsSize.height; CGFloat imageAR = imageSize.width / imageSize.height; CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise // Zooms standard portrait images on a 3.5in screen but not on a 4in screen. if (ABS(boundsAR - imageAR) < 0.17) { zoomScale = MAX(xScale, yScale); // Ensure we don't zoom in or out too far, just in case zoomScale = MIN(MAX(self.minimumZoomScale, zoomScale), self.maximumZoomScale); } } return zoomScale; }