xcode UIImageView 仅在我调用 initWithImage 时显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8497311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageView only displays when I call initWithImage
提问by Kevin_TA
For some reason, I can only display a UIImageView when I alloc/init it each iteration with a different image. What's strange is I know the image data is being loaded because I am running processing on the image and the processing is working as expected. In short, here are the two methods I was trying:
出于某种原因,我只能在每次迭代使用不同的图像分配/初始化 UIImageView 时显示它。奇怪的是我知道正在加载图像数据,因为我正在对图像进行处理并且处理按预期工作。简而言之,这是我尝试的两种方法:
// interface
@interface ViewController : UIViewController <UIAlertViewDelegate>
{
UIImageView *imageView;
}
@property (nonatomic, retain) UIImageView *imageView;
@end
// implementation
@implementation ViewController
@synthesize imageView;
//...
- (void) loadAndDisplayImage {
// Load testing image
UIImage *testImg;
testImg = [UIImage imageNamed:@"Test.png"];
self.imageView = [[UIImageView alloc] initWithImage:testImg];
//size of imageView rect
CGRect frame = self.imageView.frame;
int ivw = frame.size.width;
int ivh = frame.size.height;
//...
}
@end
When I use this method self.imageView = [[UIImageView alloc] initWithImage:testImg];
the ivw
and ivh
have valid values and the image is displayed. However, if I change the implementation to this:
当我使用此方法时self.imageView = [[UIImageView alloc] initWithImage:testImg];
,ivw
和ivh
具有有效值并显示图像。但是,如果我将实现更改为:
// implementation
@implementation ViewController
@synthesize imageView;
//...
- (void) viewDidLoad {
self.imageView = [[UIImageView alloc] init];
[self loadAndDisplayImage];
}
- (void) loadAndDisplayImage {
// Load testing image
UIImage *testImg;
testImg = [UIImage imageNamed:@"Test.png"];
self.imageView.image = testImg;
//size of imageView rect
CGRect frame = self.imageView.frame;
int ivw = frame.size.width;
int ivh = frame.size.height;
//...
}
@end
Where I am setting the image using self.imageView.image = testImg;
, the values ivw
and ivh
are both zero and no image is displayed but the subsequent processing on the image is still accurate. In both cases, I am sending the image to processing using [self doRecognizeImage:self.imageView.image];
. I can't figure out how this is possible. It would make a lot more sense to me if the processing failed when the image could not be shown.
在我使用 设置图像的地方self.imageView.image = testImg;
,值ivw
和ivh
都为零并且不显示图像,但对图像的后续处理仍然准确。在这两种情况下,我都将图像发送到使用[self doRecognizeImage:self.imageView.image];
. 我无法弄清楚这怎么可能。如果在无法显示图像时处理失败,对我来说会更有意义。
Ideas? Thanks.
想法?谢谢。
回答by occulus
The problem is that when you set the image
property on an already-init'd UIImageView
, the frame size isn't updated to match the new image size (unlike initWithImage:
).
问题在于,当您image
在已经初始化的 d 上设置属性时UIImageView
,帧大小不会更新以匹配新的图像大小(与 不同initWithImage:
)。
Whenever you have a problem like this, it's always worth checking out the docsin case you missed something:
每当您遇到这样的问题时,总是值得查看文档以防您遗漏某些内容:
Setting the image property does not change the size of a UIImageView. Call sizeToFit to adjust the size of the view to match the image.
设置图像属性不会改变 UIImageView 的大小。调用 sizeToFit 来调整视图的大小以匹配图像。
So, add a call to sizeToFit
after you've set the image property:
因此,sizeToFit
在设置图像属性后添加调用:
self.imageView.image = testImg;
[self.imageView sizeToFit];
As a side note, I'd only use the self.
dot notation when you're writing to a property, not reading it, or calling a method. In other words, you can get away with just writing:
作为旁注,我只会self.
在写入属性时使用点符号,而不是读取它或调用方法。换句话说,你可以只写:
// we are not setting imageView itself here, only a property on it
imageView.image = testImg;
// this is a method call, so "self." not needed
[imageView sizeToFit];
回答by Stoph
It might be possible that your image view isn't resizing for the image, so you're loading an image into a UIImageView with a frame of zero size. Try manually setting the frame of the image view to some other value. Something along the lines of:
您的图像视图可能没有为图像调整大小,因此您将图像加载到 UIImageView 中,框架大小为零。尝试手动将图像视图的框架设置为其他值。类似的东西:
UIImageView* test = [[UIImageView alloc] init];
[test setFrame:CGRectMake(0, 0, 100, 100)];