【问题标题】:Programmatically placing an image in an UIImageView on a UIViewController以编程方式将图像放置在 UIViewController 上的 UIImageView 中
【发布时间】:2014-02-06 22:25:50
【问题描述】:

我有一个图像(.png 文件),我想将它放在 ViewController 中的 ImageView 中。我使用以下代码,但模拟器给了我一个没有图像的空白视图。 .png 文件与 ViewController 文件位于同一目录中。代码如下:

@implementation ViewController
{
    NSArray *_pArray;
    UIImage *_image;
    UIImageView *_imageView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _image = [[UIImage alloc] initWithContentsOfFile:@"TM-1P2.png"];
    _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [_imageView setImage:_image];
    [_imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:_imageView];    
}

【问题讨论】:

  • 这个视图控制器是你应用的根视图控制器吗?您是否尝试过更改视图控制器的背景颜色以确保它出现?

标签: ios iphone objective-c uikit


【解决方案1】:

如果您在_imageNSLog 或调试器中)检查,它可能是nil。对于initWithContentsOfFile,您应该指定整个路径,例如:

NSString *path = [[NSBundle mainBundle] pathForResource:@"TM-1P2" ofType:@"png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];

或者,您可以使用以下命令,它会自动查找捆绑包中的图像:

_image = [UIImage imageNamed:@"TM-1P2.png"];

后一种语法 imageNamed 会缓存图像(即即使您关闭视图控制器也会将其保存在内存中)。如果您必须在整个应用程序中一次又一次地使用相同的图像,那就太好了(因为它不必每次都重新加载它),但如果您只使用它一次,您可能不想使用imageNamed。正如imageNamed 文档所说:

如果您有一个只显示一次的图像文件并希望确保它不会被添加到系统的缓存中,您应该使用imageWithContentsOfFile: 创建您的图像。这将使您的一次性图像远离系统图像缓存,从而可能改善您应用的内存使用特性。

注意,这两个都假设您已成功将此图像添加到您的捆绑包中。

另一方面,如果图像位于您的 Documents 文件夹中,您可以像这样加载它:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentsPath stringByAppendingPathComponent:@"TM-1P2.png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];

最后,请注意 iOS 设备区分大小写(通常模拟器不区分大小写),因此请确保您的大小写正确。


与您的问题无关,大括号之间的那些变量可能不应该在@implementation 中定义,而是您应该将它们放在@interface 中。例如,您可以将它们放在 .h 文件中,或者更好的是,您可以将它们放在 .m 文件中的私有类扩展名中,就在 @implementation 之前:

@interface ViewController ()
{
    NSArray *_pArray;
    UIImage *_image;
    UIImageView *_imageView;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"TM-1P2" ofType:@"png"];
    _image = [[UIImage alloc] initWithContentsOfFile:path];
    _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [_imageView setImage:_image];
    [_imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:_imageView];    
}

// ...

@end

【讨论】:

  • 感谢您提供的所有信息!图像名称:工作。
【解决方案2】:

您是否在调试器中单步执行?

我很想看看_image 是否为非零 - 最可能的原因是您没有将它添加到您的项目中。

【讨论】:

    【解决方案3】:

    如果您的包中有名为“TM-1P2.png”的图片,您只需执行以下操作:

    _image = [UIImage imageNamed:@"TM-1P2.png"];

    【讨论】:

    • 作为一种慷慨,您可以对所有正确的答案进行投票,除了接受最合适和/或快速的答案。
    【解决方案4】:
    - (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width,   self.view.layer.frame.size.height)];
    
    imgView.image = [UIImage imageNamed:@"emptyCart.jpeg"];
    
    imgView.backgroundColor = [UIColor whiteColor];
    
    imgView.contentMode = UIViewContentModeCenter;
    
    [self.view addSubview: imgView];
    
    }
    

    【讨论】:

    • 因为你需要替换这个:_image = [[UIImage alloc] initWithContentsOfFile:@"TM-1P2.png"];通过这个:_image = [UIImage imageNamed:@"TM-1P2.png"];
    • 所以你应该替换这一行:_image = [[UIImage alloc] initWithContentsOfFile:@"TM-1P2.png"];通过这一行:_image = [UIImage imageNamed:@"TM-1P2.png"];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多