19

I cant get my Image(.png) to display.

I check that it exists at the path I use, and it does. But no image appears.

Here is what I've been trying:

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir  = [documentPaths objectAtIndex:0];

NSString *outputPath    = [documentsDir stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];



NSString *imgPath = [[outputPath stringByReplacingOccurrencesOfString:@"mov" withString:@"png"]retain];


if ([fileManager fileExistsAtPath:imgPath]) 
{
    NSLog(@"FOUND IMG");
    NSLog(imgPath);
}


NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
[imgView setImage:thumbNail];

[cell addSubview:imgView];
[imgView release];

The debug output that confirms the file exists

FOUND IMG
2011-12-26 12:42:23.066 iGeo2[412:707] /var/mobile/Applications/1A2B0D85-CDB1-4E4B- 
9509-EF68B1A0E720/Documents/0.009000.png

I'm a little baffled. Anybody see where I've made the mistake?

Many Thanks, -Code

I've tried a few other methods, but nothing ever appears.

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • You may get your answer here: http://stackoverflow.com/questions/6663511/iphone-how-to-display-document-directory-images-in-image-view – utsabiem Dec 26 '11 at 13:00
  • To use `NSFileManager` simple use the convince method: `NSFileManager *fileManager = [NSFileManager defaultManager]` In general check the documentation for class based convince methods. Alss use ARC if possible, it really aces developing code much simpler. – zaph Dec 26 '11 at 14:27

6 Answers6

23

Try to use

NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];

or

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
YuAo
  • 1,427
  • 1
  • 10
  • 17
17

You should use [NSURL fileURLWithPath:imgPath] instead of [NSURL URLWithString:imgPath]. Image path is not a valid url, it needs a file:// prefix.

iHunter
  • 6,205
  • 3
  • 38
  • 56
4

You need to give proper file url which will be

NSURL *imgURL = [NSURL fileURLWithPath:imgPath];

then initialize your image with proper imgURL var and finally to add the image to your cell you need to do the following

[cell.contentView addSubview:imgView];

I hope it may solve your problem.

Arslan
  • 919
  • 7
  • 16
3
imageView.image = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename"]];
Bobby
  • 6,115
  • 4
  • 35
  • 36
1

Swift 4

Code:

extension FileManager {
    class func getImage(named: String) -> UIImage? {
        let imagePath = getImagePath(named: named)
        return UIImage(contentsOfFile: imagePath)
    }

    class func getImagePath(named: String) -> String {
        let fileManager = FileManager.default
        let documentDir = try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        return documentDir.appendingPathComponent(named).path
    }
}

Usage:

let image = FileManager.getImage(named: "image.png")
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

Your url is perfect.

You need to initialise image with following line

UIImage *thumbNail=[UIImage imageWithContentsOfFile:imagePath];

now do this

[imgView setImage:thumbNail];

[cell addSubview:imgView];
Pankaj purohit
  • 485
  • 3
  • 10