iOS: implement extended scale type (fit start & fit end)
This commit is contained in:
parent
3c736ace0e
commit
638f942738
@ -50,6 +50,31 @@ - (void)displayLayer:(CALayer *)layer {
|
|||||||
[super displayLayer:layer];
|
[super displayLayer:layer];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)layoutSubviews {
|
||||||
|
[super layoutSubviews];
|
||||||
|
if (self.contentMode == UIViewContentModeTopLeft || self.contentMode == UIViewContentModeBottomRight) {
|
||||||
|
UIImage *image = self.image;
|
||||||
|
if (!image) { return; }
|
||||||
|
|
||||||
|
CGSize viewSize = self.bounds.size;
|
||||||
|
CGSize imageSize = image.size;
|
||||||
|
|
||||||
|
// Calculate the scale proportionally to fit the image within the view.
|
||||||
|
CGFloat scale = MIN(viewSize.width / imageSize.width, viewSize.height / imageSize.height);
|
||||||
|
|
||||||
|
// Resize the image using the calculated scale factor.
|
||||||
|
CGSize newImageSize = CGSizeMake(imageSize.width * scale, imageSize.height * scale);
|
||||||
|
|
||||||
|
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);
|
||||||
|
[image drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height)];
|
||||||
|
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||||
|
UIGraphicsEndImageContext();
|
||||||
|
|
||||||
|
// Set the new scaled image and use UIViewContentModeTopLeft content mode.
|
||||||
|
self.image = scaledImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#elif DORIC_USE_SDWEBIMAGE
|
#elif DORIC_USE_SDWEBIMAGE
|
||||||
@ -60,6 +85,32 @@ @interface DoricImageView : SDAnimatedImageView
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation DoricImageView
|
@implementation DoricImageView
|
||||||
|
|
||||||
|
- (void)layoutSubviews {
|
||||||
|
[super layoutSubviews];
|
||||||
|
if (self.contentMode == UIViewContentModeTopLeft || self.contentMode == UIViewContentModeBottomRight) {
|
||||||
|
UIImage *image = self.image;
|
||||||
|
if (!image) { return; }
|
||||||
|
|
||||||
|
CGSize viewSize = self.bounds.size;
|
||||||
|
CGSize imageSize = image.size;
|
||||||
|
|
||||||
|
// Calculate the scale proportionally to fit the image within the view.
|
||||||
|
CGFloat scale = MIN(viewSize.width / imageSize.width, viewSize.height / imageSize.height);
|
||||||
|
|
||||||
|
// Resize the image using the calculated scale factor.
|
||||||
|
CGSize newImageSize = CGSizeMake(imageSize.width * scale, imageSize.height * scale);
|
||||||
|
|
||||||
|
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);
|
||||||
|
[image drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height)];
|
||||||
|
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||||
|
UIGraphicsEndImageContext();
|
||||||
|
|
||||||
|
// Set the new scaled image and use UIViewContentModeTopLeft content mode.
|
||||||
|
self.image = scaledImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -67,6 +118,32 @@ @interface DoricImageView : UIImageView
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation DoricImageView
|
@implementation DoricImageView
|
||||||
|
|
||||||
|
- (void)layoutSubviews {
|
||||||
|
[super layoutSubviews];
|
||||||
|
if (self.contentMode == UIViewContentModeTopLeft || self.contentMode == UIViewContentModeBottomRight) {
|
||||||
|
UIImage *image = self.image;
|
||||||
|
if (!image) { return; }
|
||||||
|
|
||||||
|
CGSize viewSize = self.bounds.size;
|
||||||
|
CGSize imageSize = image.size;
|
||||||
|
|
||||||
|
// Calculate the scale proportionally to fit the image within the view.
|
||||||
|
CGFloat scale = MIN(viewSize.width / imageSize.width, viewSize.height / imageSize.height);
|
||||||
|
|
||||||
|
// Resize the image using the calculated scale factor.
|
||||||
|
CGSize newImageSize = CGSizeMake(imageSize.width * scale, imageSize.height * scale);
|
||||||
|
|
||||||
|
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);
|
||||||
|
[image drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height)];
|
||||||
|
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||||
|
UIGraphicsEndImageContext();
|
||||||
|
|
||||||
|
// Set the new scaled image and use UIViewContentModeTopLeft content mode.
|
||||||
|
self.image = scaledImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -718,6 +795,14 @@ - (void)updateScaleType {
|
|||||||
self.view.contentMode = UIViewContentModeScaleToFill;
|
self.view.contentMode = UIViewContentModeScaleToFill;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 4: {
|
||||||
|
self.view.contentMode = UIViewContentModeTopLeft;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 5: {
|
||||||
|
self.view.contentMode = UIViewContentModeBottomRight;
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
self.view.contentMode = UIViewContentModeScaleToFill;
|
self.view.contentMode = UIViewContentModeScaleToFill;
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user