UIImageView仅在我调用initWithImage时显示 [英] UIImageView only displays when I call initWithImage

查看:57
本文介绍了UIImageView仅在我调用initWithImage时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我只能在每次迭代时使用不同的图像分配/初始化时显示UIImageView。奇怪的是我知道正在加载图像数据,因为我正在对图像运行处理并且处理按预期工作。简而言之,这是我尝试的两种方法:

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:

// 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

当我使用此方法 self.imageView = [[UIImageView alloc] initWithImage:testImg]; ivw ivh 具有有效值并显示图像。但是,如果我将实现更改为:

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:

// 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]; 将图像发送到处理中。我无法弄清楚这是怎么可能的。如果在无法显示图像时处理失败,对我来说会更有意义。

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.

想法?谢谢。

推荐答案

问题是当你设置图像属性时已初始化 UIImageView ,帧大小未更新以匹配新图像大小(与 initWithImage:不同) )。

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:).

每当遇到这样的问题时,总是值得查看 docs ,以防错过:

Whenever you have a problem like this, it's always worth checking out the docs in case you missed something:


设置image属性不会改变UIImageView的大小。
调用sizeToFit来调整视图的大小以匹配图像。

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.

因此,添加对<$ c的调用$ c> sizeToFit 设置图像属性后:

So, add a call to sizeToFit after you've set the image property:

self.imageView.image = testImg;
[self.imageView sizeToFit];

作为附注,我只使用 self。当您写入属性,不读取或调用方法时,点符号。换句话说,你只需写一下就可以了:

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:

// 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];     

这篇关于UIImageView仅在我调用initWithImage时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆