0

I am making a small project where a user can select an image and change it with filters.

I've got it working how I want, however I want all the user selected settings to return on relauch of the app. I've got all the filter settings to remain using plists.

But I can't find away of reloading the NSImage into the NSImageView after relaunch due to sandboxing - i've saved the user selected file path into a plist and have tried to load the Image in using the image URL however because of sandboxing it will not load it back.

Is there a special way I need to do this?

EDIT

Following advise to use a secure scoped bookmark i'm still having some issues

On my Awake nib I have written this - when I use NSLog on the path it is displaying the correct path but the image doesn't show in my image view.

 NSData *bookmark =[preferences objectForKey:@"bookmark"];
    NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
                                             options:NSURLBookmarkResolutionWithSecurityScope
                                     relativeToURL:nil bookmarkDataIsStale:nil error:nil];
    NSString *path = url.absoluteString;
    NSImage* imgC = [[NSImage alloc] initWithContentsOfFile: path];
    imageView2.image = imgC;

In my NSOpenPanel I have written this, so you can see how I created the bookmark

NSData *bookmark = nil;
                        NSError *error = nil;
                        bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                                 includingResourceValuesForKeys:nil
                                                  relativeToURL:nil // Make it app-scoped
                                                          error:&error];
                        if (error) {
                            NSLog(@"Error creating bookmark for URL (%@): %@", url, error);
                            [NSApp presentError:error];
                        }

                       // NSLog(@"bookmark: %@", bookmark);
                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    [userDefaults setObject:bookmark forKey:@"bookmark"];
  • 1
    You need a Security Scoped Bookmark to make access of the URL permanent or save the image in the container. – vadian Aug 05 '22 at 08:31
  • Thanks Vadian, for putting me on the right path, i've been writing some code for a security scoped bookmark following Apple Docs however while I can output the path correctly I can't seem to get the image loaded into an NSImageView - i've updated my answer - do you know where I have gone wrong? – GeraldTheGerm Aug 05 '22 at 11:59
  • Replace `url.absoluteString` with `url.path` or - better - use the URL related API `initWithContentsOfURL`. – vadian Aug 05 '22 at 12:05
  • I changed that, but still not loading anything else you can see which I may have done wrong? – GeraldTheGerm Aug 05 '22 at 12:09
  • Use `dataForKey` instead of `objectForKey`. And handle the error. – vadian Aug 05 '22 at 12:10
  • Hmm still not working i've tested using a file from bundle and it works fine displaying from bundle imageView2.image =[NSImage imageNamed:@"image"]; I've checked NSLogged the path and it's correct: /Users/Germ/Downloads/logo.png So is there something wrong with this line: imageView2.image =[[NSImage alloc] initWithContentsOfURL: url]; or is sandboxing still causing me an issue even with secure scope bookmarks? I added this as boolean com.apple.security.files.bookmarks.document-scope set to 1 in my entitlements by the way. – GeraldTheGerm Aug 05 '22 at 12:24
  • A bookmark for the Downloads folder is not necessary. Set the entitlement. And again, don’t ignore errors by passing `nil`. Pass an NSError pointer and read the error. – vadian Aug 05 '22 at 12:44
  • It's a user selected file so it may not be Download folder a user could select an image from any folder. The entitlement is already set. I've created an NSError and read it and this is what it says - not exactly helpful! The operation could not be completed. No other information is available about the problem. – GeraldTheGerm Aug 05 '22 at 13:14
  • I was outputting it wrong it came up with that error because the error was returnin nill i've fixed the error catching and there is no error - so I have no idea why it's not loading in the NSImageView – GeraldTheGerm Aug 05 '22 at 13:21
  • Figured it out forgot to do [url startAccessingSecurityScopedResource]; and [url stopAccessingSecurityScopedResource]; – GeraldTheGerm Aug 05 '22 at 14:36

0 Answers0