2011-09-10 106 views
0

這是將NSString轉換爲UIImage的正確方法嗎?將NSString轉換爲UIImage

我試過這段代碼:

NSString *localPng = [[NSBundle mainBundle] pathForResource:@"resume-1" 
                ofType:@"png"]; 

NSData* data=[localPng dataUsingEncoding:NSUTF8StringEncoding]; 

NSLog(@"datas %@",data); 

UIImage* image = [[UIImage alloc] init]; 
image = [UIImage imageWithData:data]; 

NSLog(@"-------- %@",image); 

[self uploadScoreToFaceBook:[NSString stringWithFormat:@"Medina Score %d",score] uploadImage:image ]; 

但我在image得到一個空值。

回答

1

你在做什麼是初始化一個圖像的路徑作爲數據,這顯然不工作。你可能想要做的是這樣的:

UIImage *image = [[UIImage imageWithContentsOfFile:localPng] retain]; 

你可以跳過數據部分。


完整的代碼會是這樣:

NSString *localPng = [[NSBundle mainBundle] pathForResource:@"resume-1" ofType:@"png"]; 
UIImage *image = [[UIImage imageWithContentsOfFile:localPng] retain]; 
NSLog(@"%@", image); 
[self uploadScoreToFaceBook:[NSString stringWithFormat:@"Medina Score %d", score] uploadImage:image]; 

一定要釋放你的圖像時,你不需要它了。

+0

謝謝你,多了一個問題的NSData *爲imageData = UIImagePNGRepresentation(圖像); NSString * newStr = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding]; NSLog(@「new string%@」,newStr); \t [[FacebookHelper sharedFacebookHelper] updateStatus:[NSString stringWithFormat:@「Your score is%@%@」,score,newStr]]; – user937945

+0

我想更新圖片到Facebook,有得分操作系統更新,但在圖像顯示的地方(空).. – user937945

+0

@ user937945你的應用程序包中有一個名爲'resume-1.png'的文件嗎?請注意,iPhone的文件系統是***區分大小寫的***。另外,如果'score'是一個'int',則使用'%d'或'%u'(取決於簽名)。僅在Objective-C對象中使用'%@'。 – 2011-09-10 13:34:02

0

似乎你有圖像的名稱,然後可能使用以下將有所幫助。

[UIImage imageNamed:(NSString *)name]

這裏提供圖像的名字,你會得到的圖像。

1

如果你沒有需要保留的圖像,你可以簡單地使用

UIImage *image = [UIImage imageNamed:@"resume-1.png"];