我有一个iPad应用程序(XCode 4.6, iOS 6.2, CoreData, ARC和Storyboards)。在CoreData存储中,我有一个图像。
我已经将图像写入iPad的tmp目录。这是代码:
// 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(@"nntmp directory: %@", [fileMgr contentsOfDirectoryAtPath:tmpDir error:&error]);
NSLog没有显示!
我有一个UIPopover,我在其中显示一些数据,其中包括图像。我能够从我的CoreData存储中获取图像,但在我的HTML中,它没有显示出来。这是我的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"];
知道为什么图像没有显示吗?
问题是您正在尝试使用<img>
元素,但您正在用可转换的Core Data属性的值填充其src
属性。它们的工作方式不同——img
期望一个指向图像的URL,而您的属性提供了一个UIImage
。
img
标记指向该文件。
您也可以通过使用<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM0AAAD..."
之类的东西而不是您所拥有的来实现此工作,假设您可以获得正确的格式。您可能想要原始的未转换的NSData
对象,而不是UIImage
。这可能要多做一些工作,但避免了写入临时图像文件。