问题
I create the one application in that create the locally store the file in payment while the application not be deleted.
This I have done: I download the file and store into locally (applications document folder).
Now I am having trouble with this: While the after the image is downloaded then I fetch into locally and that image will not display. In NSLog following url is generated and i can't display the image.
/Users/username/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/F233ED88-1546-4FB0-BE93-0E0FB8C8C3D6/Documents/NationalMedalofArts-150x150.jpg
Edit:
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",
@"/Users/username/Library/Application Support/iPhone Simulator/5.0/Applications/F233ED88-1546-4FB0-BE93-0E0FB8C8C3D6/Documents/NationalMedalofArts-150x150.jpg"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
NSLog(@"%@",imageData);
UIImage *image = [UIImage imageWithData:imageData];
self.imgView.image = image;
when the NSLog the NSData then it's NULL to display
回答1:
This is better:
NSString *fileName = @"NationalMedalofArts-150x150.jpg";
NSString *path = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName];
self.imgView.image = [UIImage imageWithContentsOfFile:path];
回答2:
Try using this:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* file = [documentsPath stringByAppendingPathComponent:@"NationalMedalofArts-150x150.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:file];
UIImage *image = [UIImage imageWithData:imageData];
self.imgView.image = image;
When accessing files in the documents-folder of the application, which is where images and those sorts of things are found, you must use the first line to find the path to the documents-folder. The code above should work for you.
来源:https://stackoverflow.com/questions/8799503/how-to-call-the-local-image-file-and-display-into-the-imageviewcontroller