Reputation:
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.
Upvotes: 19
Views: 24358
Reputation: 16820
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")
Upvotes: 1
Reputation: 485
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];
Upvotes: 0
Reputation: 6255
imageView.image = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename"]];
Upvotes: 3
Reputation: 1427
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];
Upvotes: 23
Reputation: 919
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.
Upvotes: 4
Reputation: 6205
You should use [NSURL fileURLWithPath:imgPath]
instead of [NSURL URLWithString:imgPath]
. Image path is not a valid url, it needs a file://
prefix.
Upvotes: 17