我正在尝试下载图像文件并将其保存到如下相册
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
我怎样才能解决这个问题?
最佳答案
我回答是因为没有人确切地告诉你为什么,而且他们在签名中毫无理由地使用大写字母。
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/