Search code examples
iosnsfilemanager

-writeToURL is not overwriting the previous write


UPDATE #1: forgot to mention this is running on an iPad app

This is my revised code (still not working, but got rid of the unnecessary code):

    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];
}

When I look at custImage in the XCode debugger, I see a different image from the previous image, which is correct. However, when it gets time to display the image at fileURL, it's a totally different image than custImage and is the same image that was displayed the first time!

UPDATE #2: I have figured out that fileURL has the correct image, but it is NOT being written to the device the second time (the first image is not being replaced).

UPDATE #3: this is the contents of htmlString that is displayed in the UIWebView popover:

<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>

Any suggestions on how to fix this?


Solution

  • I figured it out... for others that are having the same problem, here is the code that works. Notice that I do NOT save it to a temporary file, since I already have the image in the Core Data entity. Here's the code:

    -(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];
    
    }