@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;
}