-writeToURL不会覆盖先前的写入

时间:2013-10-26 19:48:18

标签: ios nsfilemanager

更新#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具有正确的图像,但第二次没有写入设备(第一张图像没有被替换)。

UPDATE#3:这是UIWebView popover中显示的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>

有关如何解决此问题的任何建议吗?

2 个答案:

答案 0 :(得分: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 
...

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

答案 1 :(得分: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];

}
相关问题