1

How can I read the raw file data from iOS's image assets?

For example an "Image" imageset containing:

  1. icon-1x.png
  2. icon-2x.png
  3. icon-3x.png

How do I use NSBundle or other methods to load the raw PNG file data (not to decode the image data) of, for example, icon-1x.png?

Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
stojanman
  • 325
  • 3
  • 15

1 Answers1

0

store the image with name icon-1x.png, icon-2x.png and icon-3x.png in image set.then try with like this loop,

for (int i = 1; i <= 3; i++) {
    NSString *imgName = [NSString stringWithFormat:@"icon-%dx.png", i];
    UIImage *img = [UIImage imageNamed:imgName];
    NSData *pngData = UIImagePNGRepresentation(img); // Convert it in to PNG data
}

otherwise you can get from tag of the imageView. then pass that tag value like this:

NSString *imgName = [NSString stringWithFormat:@"icon-%dx.png", imageView.tag];
UIImage *img = [UIImage imageNamed:imgName];
NSData *pngData = UIImagePNGRepresentation(img); // Convert it in to PNG data
Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
  • This does not help me because it does not give me the PNG data (like opening a file), but rather it decodes the image. – stojanman Mar 22 '17 at 12:57
  • Unfortunately, this does not solve my problem. The UIImagePNGRepresentation function converts the in-memory bitmap to PNG, which is different then the PNG bytes from the file. :) – stojanman Mar 23 '17 at 10:06
  • then what kind of format do you want? base64 string? – Antony Raphel Mar 24 '17 at 04:23