In the following example, I get no errors on write, but while reading, the data length is 0. Why can't I write to the shared container?
+ (UIImage *)read {
NSData *data = [NSData dataWithContentsOfFile:[self getPath]];
NSLog(@"Data length: %d", data.length);
UIImage *image = [UIImage imageWithContentsOfFile:[self getPath]];
NSLog(@"%@", image);
return image;
}
+ (void)write:(UIImage *)image {
NSString *pngFilePath = [self getPath];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
}
+ (NSString *)getPath {
NSURL *containerUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.me.app"];
NSString *pngFilePath = [[containerUrl absoluteString] stringByAppendingPathComponent:@"current.png"];
NSLog(@"%@", pngFilePath);
return pngFilePath;
}
You're mixing up URLs and file paths. Either use [containerUrl path] instead of [containerUrl absoluteString], or use dataWithContentsOfURL/writeToURL instead of dataWithContentsOfFile/writeToFile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With