2

Due to memory management issues I am using imageWithContentsOfFile instead of imageNamed, but I'm having issues accessing the images stored in Images.xcassets folder.

I have values stored in an NSDictionary that corresponds to the image name within the Asset library, i.e. image1, image2, etc.

viewDidLoad:

{
    images = @{@"01"    : @"image1",
               @"02"    : @"image2",
               @"03"    : @"image3"
               //more keys and values };

    numbers = [ [contestants allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // code to set up the cell

    NSString *number = [numbers objectAtIndex:indexPath.row];

    NSArray *nameByNumber = [images objectForKey:number];

    NSString *name = [nameByNumber objectAtIndex:indexPath.section];

    cell.imageView.image = [UIImage imageWithContentsOfFile: [ [NSBundle mainBundle] pathForResource:name ofType:@"png"] ];

    return cell;
}

However, the cell is empty, no image. I even tried to hard-code the NSString pathForResource, but nothing?

How can I achieve this using Asset Catalog?

Thanks

Pangu
  • 3,721
  • 11
  • 53
  • 120
  • Dumb question but just to be explicit: were you to use `[UIImage imageNamed:@"name"]` then the image would appear? So `+[UIImage imageNamed]` can find the thing but seemingly `-[NSBundle pathForResource:ofType:]` cannot? – Tommy Jan 29 '15 at 18:33
  • possible duplicate of [Load an image to UIImage from a file path to the asset library](http://stackoverflow.com/questions/8085267/load-an-image-to-uiimage-from-a-file-path-to-the-asset-library) – Girish Kolari Jan 29 '15 at 18:38
  • @Tommy, yes that is correct. using `[UIImage imageNamed:name]` finds the appropriate image name within the Asset Catalog but using `-[NSBundle pathForResource:ofType:]` does not, in fact `cell.imageView.image` returns `null` – Pangu Jan 29 '15 at 20:20
  • 1
    @girish there's confusion here about the term Asset Library, the question you refer to is talking about ALAssetLibrary (images in the users device photo album) This question is asking about an image in an Asset Catalog. – David Berry Jan 29 '15 at 20:29
  • Tell me more about this memory management issue and why you think how you load the image matters. – i_am_jorf Jan 29 '15 at 20:52
  • I think this is what you want http://stackoverflow.com/questions/22856283/use-xcassets-without-imagenamed-to-prevent-memory-problems/23438145#23438145 – xi.lin Dec 08 '15 at 04:38

0 Answers0