iOS dev
iOS dev

Reputation: 2284

Objective c How to convert UIImage to NSUrl

In my app I have an image getting from image picker. And I need to share that image in form of NSURL.

I tried as follows

Convert image to NSData and convert that NSData to NSURL.

UIImage *postImage = [UIImage imageNamed:@"sahredIcon.png"];
NSData *data = UIImageJPEGRepresentation(postImage, 1.0);

NSString *yourstring = [[NSString alloc] initWithData:your Data Name encoding:NSUTF8StringEncoding]; // Or any other encoding method
NSURL *url = [[NSURL alloc] initWithString:yourstring];

I causes null string exception and app crashes

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter'

Is there any way to convert UIImage to NSUrl perfectly & directly

Upvotes: 1

Views: 6647

Answers (1)

Shamsudheen TK
Shamsudheen TK

Reputation: 31339

You can't hold an UIImage in an NSURL object.Please note that an NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.

If you are looking for the Asset url, try this

-(void) imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
    NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];
}

Upvotes: 4

Related Questions