0
votes

As the title says I'm trying to take a qr code like this - http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=044779-A55W6UZD&chld=H|0 and display it as a UIImageview. I've searched but couldn't find an answer anywhere. Please help. I've tried this but it doesn't seem to work

    NSData *receivedData =  [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc] initWithData:receivedData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.frame = CGRectMake(20, 160, 200, 200);
[self.view addSubview:myImageView];
1
What doesn't work? In which method do you write this code? - Andreas
The url wont load into NSData *recievedData. If i put a NSLog right after NSData *recievedData it says its null. - Sean

1 Answers

0
votes

Try encoding the url like this and then use encodedUrl in the rest of the code:

NSString* encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

You should also remember to release the objects you created, otherwise you leak memory.

NSData* receivedData =  [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:encodedUrl]];
UIImage* image = [[UIImage alloc] initWithData:receivedData];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.frame = CGRectMake(20, 160, image.size.width, image.size.height;
[self.view addSubview:myImageView];

[receivedData release];
[image release];
[myImageView release];