【问题标题】:How to convert image format in iPad如何在 iPad 中转换图像格式
【发布时间】:2014-11-04 07:59:39
【问题描述】:

我正在研究通用应用程序。在 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 或空路径参数”。 我在谷歌上搜索了它,但没有得到任何解决方案或想法。

【问题讨论】:

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

标签: ios objective-c iphone uiimagepickercontroller nsdata


【解决方案1】:

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

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

为获得最佳实践,请尝试使用当前时间为图像命名。

【讨论】:

    【解决方案2】:

    试试下面的代码,它对我有用

    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];
    

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 2011-04-04
      • 2020-02-29
      • 2014-05-24
      • 2017-08-31
      • 2023-04-01
      • 1970-01-01
      • 2021-07-09
      相关资源
      最近更新 更多