gpt4 book ai didi

ios - UIImageView 以错误的尺寸显示

转载 作者:行者123 更新时间:2023-11-28 21:34:26 30 4
gpt4 key购买 nike

我正在将 UIImage 加载到 UIImageView 并将图像的尺寸设置为与 UIImageView 相同在 Storyboard 中创建,默认为 60x60。在代码方面,一切正常,调试显示 image 和 imageView 的尺寸均为 60px,但是查看模拟器,您显然可以看到一个矩形而不是一个盒子。问题出在哪里?

图片加载代码:

- (void)viewWillAppear:(BOOL)animated
{
NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/TheOfficialKanyeWest/picture?"];
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:userImageURL]];
UIImage* profilePic = [UIImage imageWithData:data];
profilePic = [self imageWithImage:profilePic scaledToSize:profilePictureImageView.frame.size];
[profilePictureImageView setImage:profilePic];
//[profilePictureImageView sizeToFit];

NSLog(@"\n\nView:\nWidth: %f\nHeight: %f\n\nImage:\nWidth: %f\nHeight: %f\n", profilePictureImageView.frame.size.width, profilePictureImageView.frame.size.height, profilePic.size.width, profilePic.size.height);
}

调整图像大小的自定义函数:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

此外,我在 ViewDidAppear 中进行的调试给了我:

View:
Width: 60.000000
Height: 60.000000

Image:
Width: 60.000000
Height: 60.000000

编辑:忘记添加截图

screenshot

解决方案

代码绝对没问题。我没有注意到我的自动约束导致 imageView 调整到奇怪的矩形形状。我手动添加了约束,现在效果很好。

最佳答案

imageViewwidthheight 约束固定到 Storyboard中的 60 和 60 不使用自动约束。

关于ios - UIImageView 以错误的尺寸显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491392/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com