簡體   English   中英

如何將 jpg 或 png 圖像添加到 UIImage 然后顯示在 UIImageView 中?

[英]How to add a jpg or png image to an UIImage and then display in a UIImageView?

UIImage *img = [[UIImage alloc] initWithContentsOfFile:@"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];

我已經在 IB 中連接了 UIImageView (_headersView) 的連接。 我要加載的圖像也在我的項目樹結構中。

我是否以正確的方式創建 UIImage?

如果文件在您的捆綁包中,您可以使用

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;

嘗試這個

-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:@"png"];
return path;
path = nil;
}

self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:@"123"]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM