由于某些原因,我只能在每次迭代使用不同的图像分配/初始化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
当我使用这个方法时,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
在我使用self.imageView.image = testImg;
设置图像的情况下,ivw
和ivh
的值都是零,并且没有显示图像,但是对图像的后续处理仍然是准确的。在这两种情况下,我都使用[self doRecognizeImage:self.imageView.image];
将图像发送到processing。我不明白这是怎么回事。如果在图像无法显示时处理失败,这对我来说会更有意义。
想法?谢谢。
发布于 2011-12-14 07:11:08
问题是,当您在已初始化的UIImageView
上设置image
属性时,帧大小不会更新以匹配新的图像大小(与initWithImage:
不同)。
每当你遇到这样的问题时,检查一下docs总是值得的,以防你遗漏了什么:
设置UIImageView属性不会更改图像的大小。调用sizeToFit调整视图的大小以匹配图像。
因此,在设置图像属性之后,添加一个对sizeToFit
的调用:
self.imageView.image = testImg;
[self.imageView sizeToFit];
顺便说一下,我只会在写入属性、而不是读取属性或调用方法时使用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];
发布于 2011-12-14 07:05:11
您的图像视图可能没有调整图像的大小,因此您正在将图像加载到具有零大小框架的UIImageView中。尝试手动将图像视图的边框设置为其他值。大致是这样的:
UIImageView* test = [[UIImageView alloc] init];
[test setFrame:CGRectMake(0, 0, 100, 100)];
https://stackoverflow.com/questions/8497311
复制相似问题