H.Rabiee
H.Rabiee

Reputation: 4847

UIImage with NSData initWithData is nil

The following code does not seem to load an image.

uiTabBarItem = [[UITabBarItem alloc] init];
NSData *datatmp = [NSData dataWithContentsOfFile:@"newsicon.png"];
UIImage *tmp = [[UIImage alloc] initWithData:datatmp];
uiTabBarItem.image = tmp;

datatmp is nil (0x000000) and the image does exist.

Upvotes: 0

Views: 1168

Answers (2)

GoZoner
GoZoner

Reputation: 70245

Loading an image from a file is best accomplished with:

  [UIImage imageNamed: "newsicon.png"];

Upvotes: 3

user529758
user529758

Reputation:

I. Don't reinwent the wheel. Use tmp = [UIImage imageNamed:@"newsicon.png"]; instead.

II. NSData expects a full file path when being initialized from a file. The following would work (but you don't have to use this anyway, as I just pointed it out):

NSString *iconPath = [[NSBundle mainBundle] pathForResource:@"newsicon" ofType:@"png"];
NSData *datatmp = [NSData dataWithContentsOfFile:iconPath];

Upvotes: 4

Related Questions