ios – 将UIImage调整为UIImageView

我试图将图像放入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;

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...