Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Ios 如何在iPad中转换图像格式_Ios_Objective C_Iphone_Uiimagepickercontroller_Nsdata - Fatal编程技术网

Ios 如何在iPad中转换图像格式

Ios 如何在iPad中转换图像格式,ios,objective-c,iphone,uiimagepickercontroller,nsdata,Ios,Objective C,Iphone,Uiimagepickercontroller,Nsdata,我正在研究通用应用程序。在iPhone上,一切正常,但当在iPad上测试相同的功能时,我的应用程序就会崩溃。当我从相机捕获图像时,我正在将该图像转换为jpeg格式,以下是我将图像转换为jpeg格式的代码: NSData *imageData = UIImageJPEGRepresentation(image,0.7);//0.7 [imageData writeToFile:@"Select.jpeg" atomically:YES]; 图像是UIImage。在iPhone上,它工作正常,但在

我正在研究通用应用程序。在iPhone上,一切正常,但当在iPad上测试相同的功能时,我的应用程序就会崩溃。当我从相机捕获图像时,我正在将该图像转换为jpeg格式,以下是我将图像转换为jpeg格式的代码:

NSData *imageData = UIImageJPEGRepresentation(image,0.7);//0.7
[imageData writeToFile:@"Select.jpeg" atomically:YES];
图像是UIImage。在iPhone上,它工作正常,但在iPad上,它在第二行崩溃,崩溃报告由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“***-[NSFileManager fileSystemRepresentationWithPath:]:nil或空路径参数”。
我在谷歌上搜索了它,但没有找到任何解决方案或想法。

您的文件名是Select.jpeg,该文件已存在于该位置

每次都必须使用不同的名称保存图像


要获得最佳实践,请尝试使用当前时间给出图像名称。

请尝试下面的代码,这对我很有用

NSString *strImageName = @"select.jpg";
    NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",strImageName]];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL fileExists = [fileManager fileExistsAtPath:imagePath];

    if(fileExists)
    {
        BOOL remove = [fileManager removeItemAtPath:imagePath error:nil];
        if(remove)
        {
            NSLog(@"File removed...");
        }
    }
    NSData *imageData = UIImageJPEGRepresentation(imgPreview, 0.7);
    [imageData writeToFile:imagePath atomically:YES];

您是否尝试保存另一个图像并将其命名为.ext~ipad?