Skip to content

Commit

Permalink
[fix] 兼容带有方向的UIImage数据源
Browse files Browse the repository at this point in the history
  • Loading branch information
indulgeIn committed Sep 29, 2019
1 parent 20afa8f commit 46d3b49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion YBImageBrowser/Base/YBIBAnimatedTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
42 changes: 32 additions & 10 deletions YBImageBrowser/Image/YBIBImageData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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(^{
Expand Down Expand Up @@ -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)];
Expand Down

0 comments on commit 46d3b49

Please sign in to comment.