Linked Questions

Popular Questions

Write image data in dispatch_async

Asked by At

I'm having a problem when I try to save an UIImage as a PNG file by using GCD. Here's what am I writing :


        NSString *fileName = [[NSString stringWithFormat:@"%@.png",url] substringFromIndex:5];
        dispatch_queue_t queue = dispatch_queue_create("screenshotQueue", NULL);
        dispatch_async(queue, ^{
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
            [fileManager createFileAtPath:filePath contents:UIImagePNGRepresentation(image) attributes:nil];
        });
        dispatch_release(queue);

It's working a first time but the other times I have nothing created. And this %@.png is weird because my only file created is not recognize by Finder. I have to add .png extension to the file (so filename.png.png) and I can open it then.

Related Questions