将NSString转换为UIImage

问题描述:

这是将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得到一个空值。

你在做什么是初始化一个图像的路径作为数据,这显然不工作。你可能想要做的是这样的:

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

似乎你有图像的名称,然后可能使用以下将有所帮助。

[UIImage imageNamed:(NSString *)name]

这里提供图像的名字,你会得到的图像。

如果你没有需要保留的图像,你可以简单地使用

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