2011-07-11 65 views

回答

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; 
+0

通過捆綁你的意思是項目樹權利?如果是,那麼我已經有了這兩條線,但仍然沒有圖像顯示。我還應該檢查什麼? – crzrcn

+1

對不起,如果你在'IB'中創建'UIImageView',那麼你不需要'... alloc] init',因爲它是由'IB'爲你創建的,所以只需設置'_headerView.image = [[UIImage alloc] imageWithContentsOfFile:文件路徑];' –

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"]]; 
相關問題