我UIScrollView
处理image zoomimg
。
UIImageView
resized
符合UIScrollView
(正确比例)并居中。
缩放工作完美(标准代码),但在轮换时,我需要修复imageview
,以便contentOffset
移动到新的维度。如果我根据新的旋转尺寸重新计算图像大小,则无法以正确的方式工作。
我需要将minimum zoom
更新为fit image
至UIScrollView
(我将其用于cropping
)
我已经尝试了一切,没有正确的结果。
感谢您的任何建议!
- (void)setImage:(UIImage*)image {
[self setDelegate:self];
[self.imageView setImage:image];
[self fixToImageSize];
[self centerImage];
}
#pragma mark Image
- (void)centerImage {
CGFloat offsetX = (self.bounds.size.width > self.contentSize.width)?
(self.bounds.size.width - self.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (self.bounds.size.height > self.contentSize.height)?
(self.bounds.size.height - self.contentSize.height) * 0.5 : 0.0;
self.zoomContainer.center = CGPointMake(self.contentSize.width * 0.5 + offsetX,
self.contentSize.height * 0.5 + offsetY);
}
- (void)fixToImageSize {
[self fixToImageSize:YES];
}
- (void)fixToImageSize:(BOOL)setZoomToMinZoom {
float height = size.height;
float width = size.width;
// resize image to fix max scrollview bounds
if (width > bounds.size.width) {
width = bounds.size.width;
height = bounds.size.width / (size.width / size.height);
}
if (height > bounds.size.height) {
height = bounds.size.height;
width = bounds.size.height / (size.height / size.width);
}
CGRect imageSize = CGRectMake(0, 0, width, height);
[self fixImageToSize:imageSize.size];
if (_resizeScrollViewToImage) {
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, imageSize.size.width, imageSize.size.height)];
} else if (!self.fillRectWithColor){
[self fixImageZoom:setZoomToMinZoom andSize:imageSize.size];
}
}
- (void)fixImageToSize:(CGSize)imageSize {
[self.imageView setFrame:CGRectMake(0,0, imageSize.width, imageSize.height)];
[self.zoomContainer setFrame:CGRectMake(0,0, imageSize.width, imageSize.height)];
}
- (void)fixImageZoom:(BOOL)setZoomToMinZoom andSize:(CGSize)imageSize {
float zoomOnHeight = self.bounds.size.height / imageSize.height;
float zoomOnWidth = self.bounds.size.width / imageSize.width;
float zoom = 1;
if (zoomOnHeight < 1) {
zoomOnHeight *= -1;
}
if (zoomOnWidth < 1) {
zoomOnWidth *= -1;
}
if (zoomOnHeight > zoomOnWidth) {
zoom = zoomOnHeight;
} else {
zoom = zoomOnWidth;
}
NSLog(@"Zoom on height %f",zoom);
[self setMinimumZoomScale:zoom];
if (setZoomToMinZoom && self.zoomScale < zoom) {
[self setZoomScale:zoom];
}
}
- (void)rotateImageToRight {
if (_rotateDegrees >= 360) {
_rotateDegrees = 0;
}
_rotateDegrees += 90;
//UIImage *newImage = [self.imageView.image rotateToDegrees:_rotateDegrees];
CGAffineTransform transform = CGAffineTransformMakeRotation(rad(_rotateDegrees));
[self setContentOffset:CGPointMake(0, 0) animated:YES];
[UIView animateWithDuration:0.3 animations:^{
self.imageView.transform = transform;
} completion:^(BOOL finished) {
CGSize rotatedSize = CGSizeApplyAffineTransform(self.imageView.image.size, CGAffineTransformMakeRotation(rad(_rotateDegrees)));
if (rotatedSize.width < 0) {
rotatedSize.width *= -1;
}
if (rotatedSize.height < 0) {
rotatedSize.height *= -1;
}
NSLog(@"Transformed %@",NSStringFromCGSize(rotatedSize));
float height = rotatedSize.height;
float width = rotatedSize.width;
// resize image to fix max scrollview bounds
if (width > bounds.size.width) {
width = bounds.size.width;
height = bounds.size.width / (rotatedSize.width / rotatedSize.height);
}
if (height > bounds.size.height) {
height = bounds.size.height;
width = bounds.size.height / (rotatedSize.height / rotatedSize.width);
}
rotatedSize = CGSizeMake(width, height);
NSLog(@"Transformed %@",NSStringFromCGSize(rotatedSize));
[self.zoomContainer setFrame:CGRectMake(0,0, rotatedSize.width, rotatedSize.height)];
[self fixImageZoom:YES andSize:rotatedSize];
[self centerImage];
}];
}
#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.zoomContainer;
}
- (void)scrollViewDidZoom:(UIScrollView *)aScrollView {
[self centerImage];
if (self.onZoom) {
self.onZoom(self);
}
}