Question

I have this code, who get the asset url from my image:

NSUrl *caminhoImagem;

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // Request to save the image to camera roll
    [library writeImageToSavedPhotosAlbum:[imagem CGImage] orientation:(ALAssetOrientation)[imagem imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            NSLog(@"Erro na criacao de imagem");
        } else {
            NSLog(@"Caminho da Imagem -> %@", assetURL);
            caminhoImagem = assetURL;

        }
    }];

    NSLog(@"%@",caminhoImagem);

What is happening is that the NSLog(@"%@",caminhoImagem); returns me ('null'), unless it is inserted within that method (more specifically within that Else), what I can not understand is that I made a reference using this command:

caminhoImagem = AssetUrl;

And it seems that is not saved the information, I suspect from the 'completionBlock' which in my opinion seems to be blocking something, how can I fix this, and take the information that method?

Was it helpful?

Solution

The block is executed asynchronously, so you need to call some method from inside the block to use the asset URL. You log which prints null is run before the block completes so it doesn't have access to the asset URL at that time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top