ios - 将图像保存到相册时出现 NSInvalidArgumentException

标签 ios objective-c

我正在尝试下载图像文件并将其保存到如下相册

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Downloading Started");
        NSString *urlToDownload = @"http://wallpapercave.com/wp/66iglE0.jpg";
        NSURL  *url = [NSURL URLWithString:urlToDownload];

        NSData *urlData = [NSData dataWithContentsOfURL:url];

        if ( urlData )
        {
            NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];
            NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.jpg"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [urlData writeToFile:filePath atomically:YES];
                NSLog(@"File Saved !");
                NSData *retrievedData = [NSData dataWithContentsOfFile:filePath];
               ;


                dispatch_async(dispatch_get_main_queue(), ^{
                    UIImageWriteToSavedPhotosAlbum( [UIImage imageWithData:retrievedData],
                                                   self,
                                                   @selector(done),
                                                   NULL);                });
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Failed");
            });
        }

    });

但是我收到以下错误,

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]' *** First throw call stack: (0x1816751b8 0x1800ac55c 0x18156db24 0x18c4ff9b0 0x18c500570 0x18bdd8ddc 0x100a45258 0x100a45218 0x100a4a280 0x181622810 0x1816203fc 0x18154e2b8 0x183002198 0x1875957fc 0x187590534 0x1000bcac8 0x1805315b8) libc++abi.dylib: terminating with uncaught exception of type NSException

我怎样才能解决这个问题?

最佳答案

我回答是因为没有人确切地告诉你为什么,而且他们在签名中毫无理由地使用大写字母。

来自 void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); 的文档

completionSelector
The method selector of the completionTarget object
to call. This optional method should conform to the following
signature:

- (void)image:(UIImage *)image
    didFinishSavingWithError:(NSError *)error
                 contextInfo:(void *)contextInfo;

所以你不能使用 @selector(done) 因为它不兼容,这就是你的代码崩溃的原因。

此外,请不要像其他答案那样在任何地方使用大写字母作为方法的签名: -(void)done:(UIImage *)image Error:(NSError *)error Context:(void*)context 至少应命名为 -(void)done:(UIImage *)图片错误:(NSError *)错误上下文:(void*)上下文。 而且参数的引入还是显式的好一些。

关于ios - 将图像保存到相册时出现 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43802813/

相关文章:

ios - CNContact 框架电话号码存储空白

ios - 将两个 UIImage 并排保存为一个组合图像?

ios - 如何将具有多个 segues 的 Storyboards 用于一个 View Controller ?

ios - iOS:如何获取Youtube视频流网址?

iOS - 如何使用自动布局调整 View 大小?

ios - 在运行时动态创建核心数据模型

objective-c - 移动 NSView 直到它碰到边界

ios - 苹果因为 animationDidStop :finished:context: is a non-public api 拒绝了应用

iphone - player.duration 在视频文件的 MPMoviePlayerController 中始终显示为零

iphone - iOS 7 中导航 Controller 及其 Root View Controller 的奇怪行为