获取 UIImage 以在 UIImageView 中显示 [英] Get UIImage to show in UIImageView
问题描述
我的视图没有显示.
我创建了一个空应用程序并添加了一个按钮和一个 UIImageView.
I created an empty application and added a button and an UIImageView.
我使用了故事板.只有一个故事板.一个箭头指向这个故事板.
I used storyboards. There is only one storyboard. An arrow points to this storyboard.
我在 AppDelegate 中设置了 rootViewController.我在视图控制器中设置了下面的图像.
I set the rootViewController in the AppDelegate. I set the image below in the view controller.
图像在 Images.xcassets 蓝色文件夹中列为bl_5_00".
The image is listed as 'bl_5_00' in the Images.xcassets blue folder.
一个红色按钮也没有显示,这让我相信我把其他更结构化的东西搞砸了.
A red button doesn't show either which leads me to believe I have messed something else up that is more structural.
应用委托
#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
推荐答案
如果使用
[UIImage imageWithContentsOfFile:@"bl_5_00"];
这意味着您正在尝试从绝对文件路径中获取图像,而在这里您传递的是相对名称,但这是行不通的.你可以试试.
that means you are trying to fetch the image from an absolute file path, and here you are passing relative name which will not work. You can try instead..
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];
已编辑
要检查文件是否在您的资产中,请不要尝试以下技巧只需添加
To check whether the file is in your asset or, not try the following trick just add
NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"bl_5_00" ofType:@"png"]);
如果存在,它会记录图像的完整路径,我怀疑它没有添加到你的包中,所以它不会加载,如果图像不存在,你会得到 (null).尝试重新添加图像并重新构建应用程序.
It will log the full path of the image if exist, I suspect it is not added in your bundle so its not loading, you will get (null) if image doesn't exist. Try to re-add the image and clean build the app again.
希望有帮助.干杯.
这篇关于获取 UIImage 以在 UIImageView 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!