3

更新#1:忘了提到这是在 iPad 应用程序上运行

这是我修改后的代码(仍然无法正常工作,但去掉了不必要的代码):

    NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"custImage"] URLByAppendingPathExtension:@"png"];

NSError*writeError = nil;
[client.aClientImage writeToURL:fileURL options:0 error:&writeError];
NSAssert(writeError==nil, writeError);

//  write appointment info
NSString *htmlString;
if(client.aClientEMail.length > 0)  {
    htmlString = [NSString stringWithFormat:NSLocalizedString(@"HTML_STRING1",nil),
                  client.aClientFirstName,
                  client.aClientLastName,
                  client.aClientEMail.length == 0? @"": client.aClientEMail,
                  client.aClientPrimaryPhone,
                  apptSelected.aServices,
                  fileURL];
}
else  {
    htmlString = [NSString stringWithFormat:NSLocalizedString(@"HTML_STRING2",nil),
                  client.aClientFirstName,
                  client.aClientLastName,
                  client.aClientPrimaryPhone,
                  apptSelected.aServices,
                  fileURL];
}

当我在 XCode 调试器中查看custImage时,我看到了与前一个图像不同的图像,这是正确的。但是,当需要在fileURL显示图像时,它与custImage完全不同,并且与第一次显示的图像相同

更新#2:我发现 fileURL 有正确的图像,但它没有被第二次写入设备(第一个图像没有被替换)。

更新#3:这是显示在 UIWebView 弹出窗口中的 htmlString 的内容:

<html><head><style type="text/css"> body {font-family: "Verdana"; font-size: 12;} </style></head><body>  
<h2>Rolf Marsh</h2><p>phone: 213-555-1234<p>services: Art, Decals<p><img src="file:///private/var/mobile/Applications/FEE7159E-1FF8-4B94-A446-2A4C72E0AD41/tmp/custImage.png"/></body></html>

对于如何解决这个问题,有任何的建议吗?

4

2 回答 2

1

据我所知,您永远不会将数据写入磁盘。

仅在您尝试的注释掉的部分中。

写在中间:

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"custImage"] URLByAppendingPathExtension:@"png"];    
//write 
NSError*writeError = nil;
[client.aClientImage writeToURL:fileURL options:0 error:&writeError];
NSAssert(writeError==nil, writeError);

//read / use in the html or so 
...

不要忘记重新加载 Web 视图或用于显示 html 的任何内容

于 2013-10-26T20:56:11.487 回答
0

我想通了......对于其他有同样问题的人,这里是有效的代码。请注意,我没有将其保存到临时文件中,因为我已经在 Core Data 实体中拥有了图像。这是代码:

-(void) showExistingAppointmentDetails: (AppointmentInfo *) apptSelected  {

//  make rectangle to attach popover
CGRect rectangle = CGRectMake( [apptSelected.aPosX floatValue], [apptSelected.aPosY floatValue],
                              [apptSelected.aPosW floatValue], [apptSelected.aPosH floatValue]);

//  see if this is for staff; if so, don't display anything
if([apptSelected.aApptKey isEqual: @"Staff"])
    return;

NSPredicate *predicate =  ([NSPredicate predicateWithFormat: @"aClientKey == %@", apptSelected.aApptKey ]);  //  was aApptKey
ClientInfo *client = [ClientInfo MR_findFirstWithPredicate:predicate inContext:localContext];

UIImage *image = [UIImage imageWithData:client.aClientImage];  //  image is good  <---------

//  write appointment info into html string
NSString *htmlString;
if(client.aClientEMail.length > 0)  {
    htmlString = [NSString stringWithFormat:NSLocalizedString(@"HTML_STRING1",nil),
                  client.aClientFirstName,
                  client.aClientLastName,
                  client.aClientEMail.length == 0? @"": client.aClientEMail,
                  client.aClientPrimaryPhone,
                  apptSelected.aServices,
                  [self htmlForPNGImage:image]];
}
else  {
    htmlString = [NSString stringWithFormat:NSLocalizedString(@"HTML_STRING2",nil),
                  client.aClientFirstName,
                  client.aClientLastName,
                  client.aClientPrimaryPhone,
                  apptSelected.aServices,
                  [self htmlForPNGImage:image]];

}

UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 300)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0];  //  frame color?
popoverContent.view = popoverView;

//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 300);

//  add the UIWebView for RichText
UIWebView *webView = [[UIWebView alloc] initWithFrame:popoverView.frame];
webView.backgroundColor = [UIColor whiteColor];  //  change background color here

//  add the webView to the popover
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:nil]];
[popoverView addSubview:webView];

//  if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
    [popoverController dismissPopoverAnimated:YES];
}

//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:rectangle inView:self
                 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}


- (NSString *) htmlForPNGImage:(UIImage *) image {

NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageSource = [NSString stringWithFormat:@"data:image/png;base64,%@",[imageData base64Encoding]];
return [NSString stringWithFormat:@"%@", imageSource];

}
于 2013-10-28T21:35:15.347 回答