I have an iPad app (XCode 4.6, iOS 6.2, CoreData, ARC and Storyboards). In the CoreData store, I have an image.
I have written the image to the tmp directory on the iPad. This is the code:
// create a temporary file to hold the image
NSString *tmpDir = NSTemporaryDirectory();
UIImage *custImage = [UIImage imageWithData:client.aClientImage]; // get the image
[UIImagePNGRepresentation(custImage) writeToFile:tmpDir atomically:YES]; // write the image to tmp/file
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSLog(@"\n\ntmp directory: %@", [fileMgr contentsOfDirectoryAtPath:tmpDir error:&error]);
The NSLog shows nothing!
I have a UIPopover in which I display some data which includes the image. I am able to get the image from my CoreData store, but in my HTML, it's not showing up. This is my HTML:
NSString *htmlString = [NSString stringWithFormat:NSLocalizedString(@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"Verdana\"; font-size: 12;}\n" // font family and font size here
"</style> \n"
"</head> \n"
"<body><h2>%@ %@</h2>"
"<p>email: %@<p>phone: %@<p>services: %@<p><img src=\"%@\"/>"
"</body> \n"
"</html>",nil),
client.aClientFirstName,
client.aClientLastName,
client.aClientEMail,
client.aClientPrimaryPhone,
appt.aServices,
@"tmpDir/custImage"];
Any idea why the image is not showing?
The problem is that you're attempting to use an <img>
element but you're filling in its src
attribute with the value of a transformable Core Data attribute. These don't work the same way-- img
expects a URL pointing to an image, and your attribute is providing a UIImage
.
The most straightforward way to deal with this is to write the image to a file and then make the img
tag point at that file.
You might also be able to make this work by using something like <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM0AAAD..."
instead of what you have, assuming you can get the formatting correct. You probably want the raw un-transformed NSData
object rather than the UIImage
. This is probably a little more work, but avoids writing temporary image files.