UIImageView 以错误的尺寸显示
UIImageView displaying in wrong dimensions
我正在将一个 UIImage
加载到一个 UIImageView
中,并将图像的尺寸设置为与在故事板中创建的 UIImageView
相同的尺寸,尺寸为 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
编辑:
忘记添加截图
解决方案
代码绝对没问题。我没有注意到我的自动约束导致 imageView 调整到奇怪的矩形形状。我手动添加了约束,现在效果很好。
将代码从 viewWillAppear
移至 viewDidAppear
。在 viewWillAppear
中,您的 UIImageView
未设置为最终帧。
pin width and height constraints of the imageView
to 60 and 60 in story board 不使用自动约束.
在
中尝试您的代码
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// put your code to here to set frame and size of imageview.
}
我试过你的代码,编码绝对正确,但在故事板中自动布局有任何错误..
我正在将一个 UIImage
加载到一个 UIImageView
中,并将图像的尺寸设置为与在故事板中创建的 UIImageView
相同的尺寸,尺寸为 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
编辑: 忘记添加截图
解决方案
代码绝对没问题。我没有注意到我的自动约束导致 imageView 调整到奇怪的矩形形状。我手动添加了约束,现在效果很好。
将代码从 viewWillAppear
移至 viewDidAppear
。在 viewWillAppear
中,您的 UIImageView
未设置为最终帧。
pin width and height constraints of the imageView
to 60 and 60 in story board 不使用自动约束.
在
中尝试您的代码override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// put your code to here to set frame and size of imageview.
}
我试过你的代码,编码绝对正确,但在故事板中自动布局有任何错误..