ljiatu
ljiatu

Reputation: 571

iOS - write jpeg file to file system

I'm new to iOS development and I'm trying to write an image as a jpeg file to the file system. From the logs I know that those file are indeed written to the file system, but when I try to save them to the camera roll they all appear black. I'm using the following code to write them as jpeg files:

[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpegPath atomically:YES];

And the following code to write to camera roll:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

Anybody know how to verify that those jpeg files are indeed written to the file system? And what I might be doing wrong in the second line of code?

EDIT: So here is the entire method:

- (BOOL)createImagesForSlides:(NSString *)documentsPath destinationURL:(NSURL *)destinationURL
{
    NSFileManager *manager = [NSFileManager defaultManager];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:destinationURL];
    NSString *folderName = [NSString stringWithFormat:@"/%@", document.title];

    // create new folder
    NSString *newFolderPath = [documentsPath stringByAppendingString:folderName];
    BOOL result = [manager createDirectoryAtPath:newFolderPath withIntermediateDirectories:NO attributes:nil error:nil];

    // create a jpeg file for each page of the pdf file
    for (int i = 1; i <= document.numberOfPages; ++i) {
        NSString *jpegPath = [NSString stringWithFormat:@"%@/%d.jpg", newFolderPath, i];
        [UIImageJPEGRepresentation([[document pageForPageNumber:i] image], 1.0) writeToFile:jpegPath atomically:YES];

        UIImageWriteToSavedPhotosAlbum([[document pageForPageNumber:i] image], nil, nil, nil);
    }

    return result;
}

document is a pointer to a CPDFDocument instance, and it's from some open source reader code available on github (iOS-PDF-Reader). What I basically do here is grab each page of the pdf document, generate an image, and then save them to the file system as jpeg file. Weird enough, even though there are more than 10 pages in the document, UIImageWriteToSavedPhotosAlbum only writes 5 files to camera roll. Any idea why?

Upvotes: 0

Views: 441

Answers (1)

user189804
user189804

Reputation:

It would probably be useful to see how you construct jpegPath here.

First, writeToFile:atomically: returns a BOOL, so check that for your first indication of success or failure. There are a couple of ways you can verify that the image is written to the file system. If you are running on a device use something like iExplorer to access the file system and look at the file written. Since it is NSData* you can cheek the file size to make sure it looks reasonable. On the simulator, dig into the folder structure under ~/Library/Application Support/iPhone Simulator/ and examine the file. Without looking into the filesystem itself try reading the image back into another UIImage (imageWithData: in your case since you are writing a NSData* object).

There doesn't appear to be anything wrong with your UIImageWriteToSavedPhotosAlbum call according to the docs. It is OK for the last 3 arguments to be nil (all are marked as optional), you just have to be sure the UIImage is valid. Have you set a breakpoint to be sure you have a valid image (Xcode Quick Look feature)?

Upvotes: 1

Related Questions