Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,710
6,304
I want to allow users to use a UIImagePickerController to pick either an image from a photo album or to take a picture with their camera, and then either way I want to make an NSTextAttachment with the picture.

So my code is this:

Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *url = info[UIIMagePickerControllerReferenceURL];
    if (!url) {
        // ... code omitted which saves the file and sets url to point at it ....
    } else {
        NSLog(@"The url is: %@", url);
    }

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.fileWrapper = [[NSFileWrapper alloc] initWithURL:url options:0 error:NULL];
    // ... code which inserts the attachment into the attributedString.
}

If the file has a url already, that means it came from the photo albums. It logs something like:

assets-library://asset/asset.JPG?id=DB4F5677-7FBC-4783-83A1-DEFC3C92E052&ext=JPG

Here's the problem - if I make the fileWrapper using that URL, it's garbage. It returns null for its own filename, and it doesn't seem to have any data that I can read from it. Alternatively, if I make the fileWrapper using the URL of the file that I saved, it works fine. It has a filename and I can read data from it.

The app asked for and received permission to access the user's camera roll.

So... how should I go about doing this? I'm thinking about the possibility of just taking the image and saving it to a new file either way, even if it's already in the user's camera roll, and then using that url that I've written to. But I feel like there's something I'm missing. I mean, why would Apple's APIs return a weird looking URL that their own APIs which are supposed to accept URLs can't handle?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.