2014-03-12 105 views
0

我的視圖不顯示。獲取UIImage在UIImageView中顯示

我創建了一個空應用程序,並添加了一個按鈕和一個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 

回答

1

如果使用

[UIImage imageWithContentsOfFile:@"bl_5_00"]; 

這意味着你正試圖從一個絕對的文件路徑獲取圖像,並在這裏要傳遞這是行不通的相對名稱。你可以嘗試,而不是..

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

OR

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"]); 

如果存在,它將記錄圖像的完整路徑,我懷疑這是不是在你的包添加所以它不加載,你會得到(空)如果圖像不存在。嘗試重新添加圖像,然後重新構建應用程序。

希望它有幫助。乾杯。

+1

您應該使用'imageNamed:'代替。閱讀本文http://stackoverflow.com/questions/15727313/imagewithcontentsoffile-vs-imagenamed-imagewithcontentsoffile-return-a-low-qual – elslooo

+0

應該總是避免'imageNamed'如果你有很多如果圖像加載,因爲它需要更多的內存'imageWithContentsOfFile'。是的imageNamed易於使用,但使用一個簡單的宏,你可以在處理圖像時節省大量內存,只需要在文件名中追加@ 2x。 – iphonic

+0

@Tim:我切換到ImageNamed,但它仍然不起作用。 Images.xcassets藍色文件夾中列出的圖像爲'bl_5_00'。 –

1

圖像的文件的內容不工作。您可以使用imageNamed代替:

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

我asume l_5_00是圖像的名字,你添加文件到您的項目(不只是參考)。

+0

正確,imageWithContentsOfFile需要圖像的路徑,而imageNamed只需要資源的名稱。此外,imageNamed會自動加載文件的視網膜版本(如果可用)。 – andreamazz

+0

它在Images.xcassets藍色文件夾中列出了'bl_5_00'。 –

+0

我將其更改爲imageNamed,但仍未顯示。一個紅色的按鈕並沒有顯示出哪一個讓我相信我已經搞砸了更多結構化的東西。 –