2

i have this code in my program but it seems to be crashing the program and i cant figure out why.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //Get Image URL from Library
    NSURL *urlPath = [info valueForKey:UIImagePickerControllerMediaURL];
    NSString *urlString = [urlPath absoluteString];
    NSLog(urlString);

    NSURL *root = [[NSBundle mainBundle] bundleURL];
    NSString *html;
    html = @"<img src='";
    html = [html stringByAppendingString:urlString];
    html = [html stringByAppendingString:@"' />"];
    [MemeCanvas loadHTMLString:html baseURL:root];

    [picker dismissViewControllerAnimated:YES completion:^{}];

}

It seems to be caused around the section where i append the asset-library address (urlString) to the the html string. i dont know why this would give a problem.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'

Any help is appreciated.

Thanks in advance.

X0r0N
  • 1,816
  • 6
  • 29
  • 50
  • 1
    what does it print `NSLog(urlString);` ? – Maulik Dec 17 '13 at 12:37
  • Reason is simple, you are appending nil `String`. – Bhumeshwer katre Dec 17 '13 at 12:38
  • urlString is probably nil – Mathijs Dec 17 '13 at 12:48
  • `NSString *urlString = [urlPath absoluteString];` this is clearly returning `nil` at which point I would suspect that `NSURL *urlPath = [info valueForKey:UIImagePickerControllerMediaURL];` is also returning nil. Lets try `NSLog(@"urlPath : %@", urlPath);` to see what happens and whilst your at it see what `NSLog(@"info : %@", info);` logs out as well. – Popeye Dec 17 '13 at 12:51
  • Hi. yes you are right the urlPath is null. i am able to change the UIImagePickerControllerMediaURL to UIImagePickerControllerReferenceURL... this gives me the asses address, but it wont display it on the UIWebView. do you have any ideas why it does this? – X0r0N Dec 17 '13 at 14:01
  • Why do you have two of the exact same questions? (http://stackoverflow.com/questions/20595227/how-do-i-get-an-image-from-the-ios-photo-library-and-display-it-in-in-uiwebview) – Popeye Dec 17 '13 at 14:59
  • The answer on here http://stackoverflow.com/questions/3654704/how-to-add-a-image-from-the-photo-storage-to-a-uiwebview by `Aaron Saunders` should help you. – Popeye Dec 17 '13 at 15:01

2 Answers2

1

Does this link help? It explains that you will not get a URL for images but an image object instead.

UIImagePickerControllerMediaURL always nil for a photo

Community
  • 1
  • 1
Craig Moore
  • 1,093
  • 1
  • 6
  • 15
  • Thanks, but this suggests that the image is returned as a UIImage if the item selected in the iOS photo library isn't a movie. this would be fine if it let me use this image in a way that i could put html formatting around it. do you think this is possible? – X0r0N Dec 17 '13 at 14:12
  • You cannot do that directly. The webview is a pointer to a website essentially and so the image needs to live in the source document / file that the webview is displaying, as your image does not live in the source document you cannot insert it from the webview. You need to either upload the image to a web serving location and then get the URL for that or you need to insert the image into the source document if it is coming from your device. What do you have displayed in the webview? – Craig Moore Dec 17 '13 at 15:01
  • right now there is nothing on the webview. i would add additional formatting around the image once i can get an image to display from the photo library. it may be a solution to copy the file from the photo library to a directory local to my app. i will look for a method to achieve this. any guidance on this would be appreciated. – X0r0N Dec 17 '13 at 15:12
  • 1
    This thread here discusses just that: http://www.questionhub.com/StackOverflow/3654704 – Craig Moore Dec 17 '13 at 15:19
0

I guess you need to save the image in documents directory and then display it in the webview.

Diptesh
  • 383
  • 1
  • 5
  • 14