Image from attachment from Local notification is not shown in UNNotificationContentExtension

When you create an UIImage with contentsOfFile, the UIImage object reads the image header only, which indicates basic info, such as image size, etc.

So, try move stopAccessingSecurityScopedResource to [NotificationViewController dealloc].

Or using following:

  • objective-c code:

    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];
    
  • swift code:

    let imageData = NSData(contentsOf: attachment.url)
    let image = UIImage(data: imageData! as Data)
    

There is no document saying that contentsOfFile only reads the image header. But when I run the following code:

NSString *docFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *pngPath = [docFolderPath stringByAppendingPathComponent:@"test.png"];    
UIImage *image = [UIImage imageWithContentsOfFile:pngPath];
[[NSFileManager defaultManager] removeItemAtPath:pngPath error:nil];
imageView.image = image;

An error occurs:

ImageIO: createDataWithMappedFile:1322:  'open' failed '/Users/fanjie/Library/Developer/CoreSimulator/Devices/FFDFCA06-A75E-4B54-9FC2-8E2AAE3B1405/data/Containers/Data/Application/E2D26210-4A53-424E-9FE8-D522CFD4FD9E/Documents/test.png'
     error = 2 (No such file or directory)

So I made a conclusion that UIImage contentsOfFile only reads the image header.