Demetrio Guilardi
Demetrio Guilardi

Reputation: 325

swift - UIImageWriteToSavedPhotosAlbum causes EXC_BAD_ACCESS

I am saving a image in the camera roll. But when it is done, I should make a reference to the image in the DB. The logic is pretty simple

class MyDownloader{
    let db : DbHelper = DbHelper.sharedInstance

    func downloadFileFromServer(){
        let urlString = "http://any-image.jpg"
        let url = NSURL(string: urlString)
        let data = NSData(contentsOfURL: url!)
        let image = UIImage(data: data!)
        if(image == nil){
            println("no downloaded image")
        }
        else{
            // THIS WORKS WITH NO ERRORS
            --> --> UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

            // THIS NEVER WORKS 
            --> --> UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
        }
    }

    func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
        if error != nil {
            // error feedback goes here
        }
        else{
            // save db reference here
        }
    }
}

The method below

UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)

always returns EXC_BAD_ACCESS

Upvotes: 1

Views: 1418

Answers (2)

Evan
Evan

Reputation: 442

You probably should make MyDownloader inherit from NSObject as selector is part of Objective-C's runtime.

Upvotes: 1

Andrew M
Andrew M

Reputation: 651

Your MyDownloader class instance is probably being deallocated before the UIImageWriteToSavedPhotosAlbum call finishes, so when it tries to call the image:didFinishSavingWithError:contextInfo: method, the callback target no longer exists. Make sure that your MyDownloader object sticks around.

Upvotes: 0

Related Questions