我试图将图像放入uiimageview,图像下载并加载dinamically,并且只有一个分辨率可用于ios和
Android应用程序.
因此,我需要图像来保持宽高比和比例宽度,我将UIImageView内容模式设置为
UIViewContentModeScaleAspectFill,但它将图像居中,因此它会从屏幕上移出顶部和底部,图像将被设计为不需要底部.
如何将图像对齐左上角?
并且UIImageView能为我扩展到宽度吗?或者我该怎么办?
提前致谢.
编辑:
我尝试了setcliptobounds并将图像切割为imageview大小,这不是我的问题.
UIViewContentModetopLeft运行良好,但现在我无法应用UIViewContentModeScaleAspectFill,或者我可以同时应用它们吗?
解决方法
您可以缩放图像以适合图像视图的宽度.
您可以在UIImage上使用一个类别来创建具有所选宽度的新图像.
@interface UIImage (Scale)
-(UIImage *)scaletoWidth:(CGFloat)width;
@end
@implementation UIImage (Scale)
-(UIImage *)scaletoWidth:(CGFloat)width
{
UIImage *scaledImage = self;
if (self.size.width != width) {
CGFloat height = floorf(self.size.height * (width / self.size.width));
CGSize size = CGSizeMake(width,height)
// Create an image context
UIGraphicsBeginImageContext(size);
// Draw the scaled image
[self drawInRect:CGRectMake(0.0f,0.0f,size.width,size.height)];
// Create a new image from context
scaledImage = UIGraphicsGetimageFromCurrentimageContext();
// Pop the current context from the stack
UIGraphicsEndImageContext();
}
// Return the new scaled image
return scaledImage;
}
@end
这样您就可以使用它来缩放图像
UIImage *scaledImage = [originalImage scaletoWidth:myImageView.frame.size.width]; myImageView.contentMode = UIViewContentModetopLeft; myImageView.image = scaledImage;