2011-07-13 63 views
21

如何在硬盤中顯示UIImageView組件中的圖像?在UIImageView中顯示本地圖像

我有一個名爲「圖像」的本地文件夾,並希望通過相對路徑訪問它,因爲我想要將圖像與應用程序打包在一起。

回答

52

的文檔從某處加載圖片上的驅動器的使用:

UIImage * myImage = [UIImage imageWithContentsOfFile: @"../images/1.png"]; 

或者添加圖片到Xcode項目及用途:

UIImage * myImage = [UIImage imageNamed: @"1.png"]; 

之後, DD圖像的ImageView:

UIImageView * myImageView = [[UIImageView alloc] initWithImage: myImage]; 
+2

既然你想打包你的應用程序,你應該使用這裏的最後兩行代碼,而不是最上面的一行。您還應該將該映像從硬盤驅動器拖到項目中的文件夾中,例如Resources文件夾。 – tazboy

10

只要圖像在您的資源文件夾中,您不需要提供任何路徑。

您可以使用它在imageView中顯示該圖像。

yourImageView.image = [UIImage imageNamed:@"something.png"]; 
+0

沒想到你需要把這個擴展與這種方法? – zonabi

1
UIImage *img = [UIImage imageWithContentsOfFile:(the file path)]; 
UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 

支票imageWithContentsOfFile:

0

你說的硬drive.Are你說你一直保持你的形象在iphone app.If你的包,那麼你可以作出這樣的圖像陣列圖像的意思...

NSMutableArray *imageArray; 
imageArray=[[NSMutableArray alloc]initWithObjects:@"001.jpg",@"002.jpg",@"003.jpg",@"004.jpg",nil]; 

然後可以通過這個數組訪問你的圖像;

或者,如果你只是想表明從包一個單一的形象,你可以做這樣

UIImageView *creditCardImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 16, 302, 77)]; 
    creditCardImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"CreditCard_2RowTable" ofType:@"png"]]; 
    [self.view addSubview:creditCardImageView]; 

這將幫助你。

相關問題