Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用模拟器ios 8将UIImage保存到磁盘';行不通_Objective C_File_Ios8 - Fatal编程技术网

Objective c 使用模拟器ios 8将UIImage保存到磁盘';行不通

Objective c 使用模拟器ios 8将UIImage保存到磁盘';行不通,objective-c,file,ios8,Objective C,File,Ios8,以下代码在ios

以下代码在ios<8上运行良好,但在xcode 6.1.1和ios 8设备模拟器上运行不正常

NSData *imageData = UIImagePNGRepresentation(shareSnapshot);
[imageData writeToFile:@"/Users/MyUser/Desktop/123.png" atomically:YES];

有人知道这是否是一个模拟器问题吗?我没有一个带ios8的真实设备要检查。

所以你在这里遇到了权限问题。我不知道为什么它在8.0之前工作,但我看到(一些明显的变化):

无法完成该操作。(错误513)

目前,在实际设备上运行时,您将遇到问题。更好的处理方法是将数据保存到应用程序的文档目录中

  • 通过NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)获取目录路径
  • 使用stringByAppendingPathComponent追加文件名
  • 然后,您可以通过访问模拟器应用程序目录来验证它是否正确保存:

    • /用户/用户/库/开发者/核心模拟器/设备//文档/

    我记得由于安全和沙盒原因,您不能为iOS提供“/Users/MyUser/Desktop/”这样的路径

    退房

    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,我很乐意在这方面进行测试。