作者:清清果冻儿 | 来源:互联网 | 2023-05-28 12:54
在下面的例子中,我没有写入错误,但在读取时,数据长度为0.为什么我不能写入共享容器?
+ (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;
}
Bart Whitele..
8
您正在混淆URL和文件路径.使用[containerUrl path]而不是[containerUrl absoluteString],或使用dataWithContentsOfURL/writeToURL而不是dataWithContentsOfFile/writeToFile.
1> Bart Whitele..:
您正在混淆URL和文件路径.使用[containerUrl path]而不是[containerUrl absoluteString],或使用dataWithContentsOfURL/writeToURL而不是dataWithContentsOfFile/writeToFile.