我的視圖不顯示。獲取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
您應該使用'imageNamed:'代替。閱讀本文http://stackoverflow.com/questions/15727313/imagewithcontentsoffile-vs-imagenamed-imagewithcontentsoffile-return-a-low-qual – elslooo
應該總是避免'imageNamed'如果你有很多如果圖像加載,因爲它需要更多的內存'imageWithContentsOfFile'。是的imageNamed易於使用,但使用一個簡單的宏,你可以在處理圖像時節省大量內存,只需要在文件名中追加@ 2x。 – iphonic
@Tim:我切換到ImageNamed,但它仍然不起作用。 Images.xcassets藍色文件夾中列出的圖像爲'bl_5_00'。 –