Rahul Sharma
Rahul Sharma

Reputation: 950

How To convert URL link into UIImage in iOS?

I am Using UIImage View and add Image there like this code

 NSURL *imageURL = [NSURL URLWithString:@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001 123.jpg"];
NSLog(@"imge %@",imageURL);

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    NSLog(@"imge %@",imageData);
UIImage *image = [UIImage imageWithData:imageData];
   NSLog(@"imge %@",image);
[image_view setImage:image];

but all of nslog show null Actually there is problem in url but this show image on web so please tell me what's the problem here.

Upvotes: 1

Views: 6412

Answers (2)

Mital Solanki
Mital Solanki

Reputation: 171

Try Following Code :

NSURL *aURL = [NSURL URLWithString:[@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001%20123.jpg" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

UIImage *aImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];

This is working fine...!!!

Upvotes: 5

girish_vr
girish_vr

Reputation: 3061

Your URL string has a space which needs to be replaced with percent escapes.

Either use this -

NSURL *imageURL = [NSURL URLWithString:@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001%20123.jpg"];

Or

NSString *imageUrlString = @"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001 123.jpg";
NSString *encodedImageUrlString = [imageUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageURL = [NSURL URLWithString: encodedImageUrlString];

Upvotes: 2

Related Questions