2

im trying to download an image file and save it to phot albums as following

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");
            });
        }

    });

But im getting the following error,

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

How will i be able to sort this out?

  • Which line exactly is causing the issue? `UIImageWriteToSavedPhotosAlbum()` one? What's the code of `-done`? – Larme May 05 '17 at 10:41
  • @Hara Hara Mahadevaki: Add an exception breakpoint to get the exact line where the crash is taking place. – iPeter May 05 '17 at 10:46

5 Answers5

7

I'm answering because no one tells you exactly WHY, and they use uppercase with no reason in the signature.

From the doc of 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;

So you can't use @selector(done) because it's not compliant, and that's why your code crash.

Also, please do not use uppercase wherever you want for the signature of the method like the other answers: -(void)done:(UIImage *)image Error:(NSError *)error Context:(void*)context should be named at least -(void)done:(UIImage *)image error:(NSError *)error context:(void*)context. And it may be better to explicit the introduction of the parameters.

Community
  • 1
  • 1
Larme
  • 24,190
  • 6
  • 51
  • 81
1

Replace your done function as below

-(void)done:(UIImage *)image Error:(NSError *)error Context:(void*)context{

}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
1

Working in Swift 5.3.2:

@objc func done(image: UIImage, didFinishSavingWithError: NSError, contextInfo: UnsafeRawPointer?) {
    // TODO
}

Not really sure if the type of contextInfo is correct.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jake A.
  • 540
  • 5
  • 12
0
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 )
    {
        dispatch_async(dispatch_get_main_queue(), ^{
          UIImageWriteToSavedPhotosAlbum( [UIImage imageWithData:urlData],
                                               self,
                                               @selector(done),
                                               NULL);               
 });
    } else {
         NSLog(@"Failed");
    }
});

Try this code save data directly on gallary because you are getting image data no need to save locally and retry that again

Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
0

hi @Hara Hara Mahadevaki: the error is coming of the selector you add while saving the image can you try this

UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:retrievedData], self, @selector(saveImage:Error:Context:), nil);

method call after completion

- (void)saveImage:(UIImage *)image Error:(NSError *)error Context:(void*)context {
    if (error) {
        // error comes
    } else {
        // image successfully save
    }
}
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13