【问题标题】:Get UIImage to show in UIImageView获取 UIImage 在 UIImageView 中显示
【发布时间】:2014-03-12 18:28:54
【问题描述】:

我的视图没有显示。

我创建了一个空应用程序并添加了一个按钮和一个 UIImageView。

我使用了故事板。 只有一个故事板。 一个箭头指向这个故事板。

我在 AppDelegate 中设置了 rootViewController。我在视图控制器中设置了下面的图像。

图像在 Images.xcassets 蓝色文件夹中列为“bl_5_00”。

红色按钮没有显示,这让我相信我搞砸了其他更具结构性的东西。

应用代理

#import "PEPI_AppDelegate.h"
#import "PEPI_LessonViewController.h"

@implementation PEPI_AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    PEPI_LessonViewController *controller = [[PEPI_LessonViewController alloc] init];
    self.window.rootViewController = controller;

    [self.window makeKeyAndVisible];
    return YES;
}

视图控制器

#import "PEPI_LessonViewController.h"

@interface PEPI_LessonViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *mainImage;

@end

@implementation PEPI_LessonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImage *imageYouWantToPass = [UIImage imageNamed:@"bl_5_00"];

    self.mainImage.image = imageYouWantToPass;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

    标签: ios objective-c uiimageview uiimage


    【解决方案1】:

    如果你使用

    [UIImage imageWithContentsOfFile:@"bl_5_00"];
    

    这意味着您正在尝试从绝对文件路径获取图像,而在这里您传递的相对名称将不起作用。你可以试试..

    NSString *filePath=[[NSBundle mailBundle] pathForResource:@"bl_5_00" ofType:@"png"];
    

    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"bl_5_00.png"];
    

    然后

    UIImage *image=[UIImage imageWithContentsOfFile:filePath];
    

    已编辑

    要检查文件是否在您的资产中,或者不尝试以下技巧 只需添加

    NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"bl_5_00" ofType:@"png"]);
    

    如果存在,它将记录图像的完整路径,我怀疑它没有添加到你的包中,所以它没有加载,如果图像不存在,你会得到 (null)。尝试重新添加图像并重新构建应用程序。

    希望对您有所帮助。干杯。

    【讨论】:

    • 您应该改用imageNamed:。阅读此stackoverflow.com/questions/15727313/…
    • imageNamed 如果您有很多图像加载,则应始终避免使用,因为它比imageWithContentsOfFile 占用更多内存。是的,imageNamed 易于使用,但使用简单的宏实际上可以在处理图像时节省大量内存,只需在文件名中附加 @2x。
    • @Tim:我切换到 ImageNamed 但它仍然无法正常工作。列出的图像在 Images.xcassets 蓝色文件夹中有“bl_5_00”。
    • @iphonic imageNamed: 缓存图像(因此内存中只有一个实例),因此如果您多次加载同一个图像,它实际上使用的内存比imageWithContentsOfFile: 少得多(如果你不这样做,那么它仍然使用相同数量的内存)。
    • @GuidoAnselmi 检查我编辑的答案。希望对您有所帮助。
    【解决方案2】:

    如果您传递图像名称,则带有文件内容的图像将不起作用。您可以改用 imageNamed:

    UIImage *imageYouWantToPass = [UIImage imageNamed:@"bl_5_00"];
    

    我假设 l_5_00 是图像的名称,并且您将此文件添加到您的项目中(不仅仅是参考)。

    【讨论】:

    • 正确,imageWithContentsOfFile 需要图像的路径,而 imageNamed 只需要资源的名称。此外,imageNamed 会自动加载文件的视网膜版本(如果有)。
    • 它在 Images.xcassets 蓝色文件夹中列出了“bl_5_00”。
    • 我将其更改为 imageNamed 但它仍然不显示。红色按钮也没有显示,这让我相信我搞砸了其他更具结构性的东西。
    • 我在您的代码中看不到任何对 redButton 的引用。你有没有从故事板做 mainImage 插座连接?尝试从您的项目中删除图像(文件)并重新添加,记得选中将图像复制到目标...复选框。
    • @Greg 是的,我通过 CNTL+Click+Drag to Code 使用 IBOutlet 建立了连接。图像都在 Images.xcassets 中。这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2013-10-13
    • 1970-01-01
    相关资源
    最近更新 更多