Objective c 使用模拟器ios 8将UIImage保存到磁盘';行不通
以下代码在ios<8上运行良好,但在xcode 6.1.1和ios 8设备模拟器上运行不正常Objective c 使用模拟器ios 8将UIImage保存到磁盘';行不通,objective-c,file,ios8,Objective C,File,Ios8,以下代码在ios
NSData *imageData = UIImagePNGRepresentation(shareSnapshot);
[imageData writeToFile:@"/Users/MyUser/Desktop/123.png" atomically:YES];
有人知道这是否是一个模拟器问题吗?我没有一个带ios8的真实设备要检查。所以你在这里遇到了权限问题。我不知道为什么它在8.0之前工作,但我看到(一些明显的变化): 无法完成该操作。(错误513) 目前,在实际设备上运行时,您将遇到问题。更好的处理方法是将数据保存到应用程序的文档目录中
- /用户/用户/库/开发者/核心模拟器/设备//文档/
NSSearchPathForDirectoriesInDomains(nsCachesDirectority,NSUserDomainMask,是)代码>
找到合适位置的文档
NSSearchPathDirectory
定义了许多可以使用的路径。希望这有帮助试试这个
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Now I use the "screenshot"
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/image.jpg"];
if ([UIImageJPEGRepresentation(image, 1) writeToFile:path atomically:YES]) {
NSLog(@"save ok");
}
else {
NSLog(@"save failed");
}
Swift 3.0
// Create path.
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let filePath = "\(paths[0])/MyImageName.png"
// Save image.
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)
你没有真正的磁盘路径。我尝试过结果是一样的,bellow IOS 8可以正常工作,但IOS 8和upYou肯定有权限写入文档目录。您可能需要构建一个小的示例项目来实现这一点。请随意将该示例推送到Github,我很乐意在这方面进行测试。