开发者

Why won't this image show up? Using NSData and initWithData

开发者 https://www.devze.com 2023-02-16 03:14 出处:网络
NSURL* urlEx = [NSURL URLWithString:@\"Pacific_Map.png\"]; NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];
NSURL* urlEx = [NSURL URLWithString:@"Pacific_Map.png"];
NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];

UIImage* imgEx = [[UIImage alloc] initWithData:mapExIMGData];

mapImageViewEx.image = imgEx;

If I replace mapImageViewEx.image = imgEx; with mapImageViewEx.image = [UIImage imageNamed:@"Pacific开发者_Go百科_Map.png"]; it works, but I do not want to use imageNamed.


That's because imageNamed knows where to scan for the file, but initWithContentsOfURL expects the full path. use

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"Pacific_Map" ofType:@"png"];
NSURL* urlEx = [NSURL fileURLWithPath:imagePath];
NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];

UIImage* imgEx = [[UIImage alloc] initWithData:mapExIMGData];

mapImageViewEx.image = imgEx;


What's wrong with using imageNamed? Anyway, you could use initWithContentsofPath.

+ (UIImage *)imageWithContentsOfFile:(NSString *)path

Tell us more about what you are trying to accomplish. Good luck,

James

EDIT: Sorry, I assumed Pacific_Map.png was just a placeholder path. Like someone else posted, you need to indicate the full path if you're not going to use imageNamed.

0

精彩评论

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

关注公众号