ios - 以编程方式将图像放置在 UIViewController 上的 UIImageView 中

标签 ios iphone objective-c uikit

我有一个图像(一个 .png 文件),我想将它放在 ViewController 中的 ImageView 中。我使用以下代码,但模拟器给了我一个没有图像的空白 View 。 .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];    
}

最佳答案

如果您在 _image 进行检查(NSLog 或在调试器中),它可能是 nil .与 initWithContentsOfFile您应该指定整个路径,例如:

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

或者,您可以使用以下内容,它会自动在包中查找图像:
_image = [UIImage imageNamed:@"TM-1P2.png"];

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

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.



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

另一方面,如果图像在您的 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 文件中的私有(private)类扩展名中,就在 @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

关于ios - 以编程方式将图像放置在 UIViewController 上的 UIImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615406/

相关文章:

iOS 8 UIActivityViewController 和 UIAlertController 按钮文本颜色使用窗口的 tintColor

ios - 在应用程序 IOS Objective C 启动时添加新的 View Controller

ios - 如何删除 Xcode 警告 Apple Mach-O Linker Warning 'Pointer not aligned at address

ios - 使用 UIPIckerView 自定义 DatePicker

ios - 如何在 iOS 7 中更改 UITabBar 外观

iphone - 如何将小写字符转换为大写字符?

ios - 无法在装有 iOS 5.1.1 的设备上测试 App?

ios - 如何在 Safari 和 iOS 11 上播放 WebRTC 媒体流

iphone - 为 iPhone 编译 ffmpeg 时出错

iphone - 打开 'DEBUG'宏值