2

在此处输入图像描述

代码片段

        self.newsImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,300,130)];
        //set placeholder image or cell won't update when image is loaded
        self.newsImage.image = [UIImage imageNamed:@"newsDetail.png"];
        //load the image
        self.newsImage.imageURL = [NSURL URLWithString:imageBig];
        [imageBack addSubview:self.newsImage];

我有一张 40*40 大小的图像,但图像视图大小为 300*130。如何避免拉伸图像。

我想要 UIImageview 的中心。

提前致谢 !!!!

4

2 回答 2

2

只需将内容居中:

self.newsImage.contentMode = UIViewContentModeCenter;
于 2013-07-02T13:23:28.913 回答
1

您必须将 CGSize 设置为您的图像宽度和高度,以便图像不会拉伸并且它安排在中间。

- (UIImage )imageWithImage:(UIImage )image scaledToFillSize:(CGSize)size
{
    CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
    CGFloat width = image.size.width * scale;
    CGFloat height = image.size.height * scale;
    CGRect imageRect = CGRectMake((size.width - width)/2.0f,
                                  (size.height - height)/2.0f,
                                  width,
                                  height);

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    [image drawInRect:imageRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
于 2016-08-11T09:37:52.677 回答