-writeToURL 不会覆盖之前的写入

Posted

技术标签:

【中文标题】-writeToURL 不会覆盖之前的写入【英文标题】:-writeToURL is not overwriting the previous write 【发布时间】:2013-10-26 19:48:18 【问题描述】:

更新 #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 完全不同,而 与第一次显示的图像相同 em>!

更新 #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>

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

【问题讨论】:

【参考方案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 的内容

【讨论】:

我认为您非常接近,但这并没有解决它...我知道** custImage** 是正确的,因为我可以在调试器中看到它。让我更新原始问题中的代码(我发现很多代码没有做任何事情) 确保您可以在磁盘上的 finder 中看到 custImage Daij-Djan:这是在 iPad 应用程序上运行的...没有查找器:- 看看它是否在磁盘上被改变了。至少在 ios 模拟器中【参考方案2】:

我想通了...对于遇到相同问题的其他人,这是有效的代码。请注意,我没有将其保存到临时文件中,因为我已经在 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];


【讨论】:

以上是关于-writeToURL 不会覆盖之前的写入的主要内容,如果未能解决你的问题,请参考以下文章

怎么做到Python file重复写入之前的内容不被后写入的覆盖

python如何写入文本的最末行?

写入文本文件而不覆盖它

labview中字符串写入txt时怎么做到不覆盖?

覆盖 CPP 输出中的行标记文件名

document.write 向文档中写内容,包括文本脚本元素之类的,但是它在什么时候执行不会覆盖当前页面内容尼?