diff --git a/YBImageBrowser/Base/YBIBAnimatedTransition.m b/YBImageBrowser/Base/YBIBAnimatedTransition.m index 4b65d7d..6ff97c9 100644 --- a/YBImageBrowser/Base/YBIBAnimatedTransition.m +++ b/YBImageBrowser/Base/YBIBAnimatedTransition.m @@ -83,7 +83,7 @@ - (void)yb_showTransitioningWithContainer:(UIView *)container startView:(__kindo } completion:^(BOOL finished) { completion(); // Disappear smoothly. - [UIView animateWithDuration:0.25 animations:^{ + [UIView animateWithDuration:0.2 animations:^{ animateImageView.alpha = 0; } completion:^(BOOL finished) { [animateImageView removeFromSuperview]; diff --git a/YBImageBrowser/Image/YBIBImageData.m b/YBImageBrowser/Image/YBIBImageData.m index 03f2124..b7e4f12 100644 --- a/YBImageBrowser/Image/YBIBImageData.m +++ b/YBImageBrowser/Image/YBIBImageData.m @@ -200,7 +200,7 @@ - (void)loadImageBlock { // Do not need to decode If 'image' conformed 'YYAnimatedImage'. (Not entirely accurate.) if (![image conformsToProtocol:@protocol(YYAnimatedImage)]) { CGImageRef cgImage = YYCGImageCreateDecodedCopy(image.CGImage, shouldPreDecode); - image = [UIImage imageWithCGImage:cgImage]; + image = [UIImage imageWithCGImage:cgImage scale:image.scale orientation:image.imageOrientation]; if (cgImage) CGImageRelease(cgImage); } YBIB_DISPATCH_ASYNC_MAIN(^{ @@ -383,26 +383,48 @@ - (void)cuttingImageToRect:(CGRect)rect complete:(void (^)(UIImage * _Nullable)) complete(nil); return; } - // Physical pixel. CGFloat scale = self.originImage.scale; - CGRect rectOfPhysical = rect; - rectOfPhysical.origin.x *= scale; - rectOfPhysical.origin.y *= scale; - rectOfPhysical.size.width *= scale; - rectOfPhysical.size.height *= scale; - CGImageRef cgImage = CGImageCreateWithImageInRect(self.originImage.CGImage, rectOfPhysical); + CGFloat width = self.originImage.size.width; + CGFloat height = self.originImage.size.height; + + BOOL reverseWidthHeight = NO; + CGAffineTransform transform = CGAffineTransformIdentity; + switch (self.originImage.imageOrientation) { + case UIImageOrientationDown: + case UIImageOrientationDownMirrored: + CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI), -width, -height); + break; + case UIImageOrientationLeft: + case UIImageOrientationLeftMirrored: + transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI_2), 0, -height); + reverseWidthHeight = YES; + break; + case UIImageOrientationRight: + case UIImageOrientationRightMirrored: + transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI_2), -width, 0); + reverseWidthHeight = YES; + break; + default: break; + } + transform = CGAffineTransformScale(transform, scale, scale); + CGRect correctRect = CGRectApplyAffineTransform(rect, transform); + CGImageRef cgImage = CGImageCreateWithImageInRect(self.originImage.CGImage, correctRect); + if (isCancelled()) { complete(nil); if (cgImage) CGImageRelease(cgImage); return; } - CGSize size = [self bestSizeOfCuttingWithOriginSize:CGSizeMake(CGImageGetWidth(cgImage) / scale, CGImageGetHeight(cgImage) / scale)]; - UIImage *tmpImage = [UIImage imageWithCGImage:cgImage]; + CGFloat cgWidth = reverseWidthHeight ? CGImageGetHeight(cgImage) : CGImageGetWidth(cgImage); + CGFloat cgHeight = reverseWidthHeight ? CGImageGetWidth(cgImage) : CGImageGetHeight(cgImage); + CGSize size = [self bestSizeOfCuttingWithOriginSize:CGSizeMake(cgWidth / scale, cgHeight / scale)]; + UIImage *tmpImage = [UIImage imageWithCGImage:cgImage scale:self.originImage.scale orientation:self.originImage.imageOrientation]; if (isCancelled()) { complete(nil); if (cgImage) CGImageRelease(cgImage); return; } + // Ensure the best display effect. UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale); [tmpImage drawInRect:CGRectMake(0, 0, size.width, size.height)];