开发者

Converting NSString to UIImage

Is this the correct way to convert a NSString to a UIImage?

I tried this code:

NSString *localPng = [[NSBundle mainBundle] pathForResource:@"resume-1"
                                                     ofType:@"p开发者_JAVA百科ng"];   

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 ];

But I am getting a null value in image.


If you do not need to retain the image you can simply use

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


What you are doing is initializing an image with a path as data, which does't work obviously. What you might want to do instead is this:

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

You can skip the data part.


The complete code would be this:

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];

Be sure to release your image when you don't need it anymore.


Seems that you have name of the image then probably using following will help.

[UIImage imageNamed:(NSString *)name]

provide name of the image here and you will get image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜