UIImage *img = [[UIImage alloc] initWithContentsOfFile:@"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];
我已经在IB中连接了UIImageView(_headersView)的连接。我想加载的图像也在我的项目树结构中。
我是否以正确的方式创造了UIImage?
答案 0 :(得分:8)
如果文件在你的包中,你可以使用
UIImage *img = [UIImage imageNamed:@"contactHeader"];
_headersView = [[UIImageView alloc] initWithImage:img];
如果图像位于文档目录中,则可以使用
// Get the path to your documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add the file to the end of the documents path
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"contactHeader.png"];
UIImage *img = [[UIImage alloc] imageWithContentsOfFile:filePath];
_headersView = [[UIImageView alloc] initWithImage:img];
[img release]; img = nil;
答案 1 :(得分:2)
试试这个
-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:@"png"];
return path;
path = nil;
}
self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:@"123"]];