Load qr code from url and display as image view - iphone

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];

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];

Related

Refresh view or remove subviews ios

I'm trying to make a scene where the user can swipe over the screen to browse between posts. The post can be both an image with text or just a note, and the view is altered depending on which one occurs.
The getting process works just perfect. It gets the right post whether i swipe right or left. The problem is that the old view wont disappear and the views are overlapping. This is especially bothering when you go from a note to a photo or vice versa since the sizes ar different...
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#" GETRESULT %# POSTNUMBER %d", getResult, postNumber);
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[[self view] addGestureRecognizer:recognizerRight];
UISwipeGestureRecognizer *recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizerLeft];
// Do any additional setup after loading the view.
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE RIGHT");
if(postNumber > 0){
postNumber--;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if (getImageString) {
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE LEFT");
if(postNumber < [getResult count] - 1){
postNumber++;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if(getImageString){
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
At first i only increased postNumber and called viewDidLoad in the swipe actions. That gave me the exact same result though... This is only one of countless trial and error attempts, and I'm sorry this is among the messier ones...
Would greatly appreciate it if anyone got a solution.
Thanks in advance,
Tom
Did you try this?
for (UIView * view in self.view.subviews) {
if ([view isEqual:postText]||[view isEqual:imgView]) {
[view removeFromSuperview];
}
}
I couldn't remove the variables from the superview because I did it in a new instance of viewDidLoad. I tried to do so before calling viewDidLoad this time and it worked!
Sorry for wasting your time...
Tom

iphone: Three20 library's MockPhoto with server images

I am trying to download images from server and display in 320 library's TTThumbsViewController. I am not able to get this working!
Following works for me when I have images locally stored:
//Create thumbnails
[ImageUtils checkAndSaveThumbnail:imageName andSize:CGSizeMake(100.0, 100.0) andLocation:NO andPrefixof:#"small"];
NSString *fullImageName = [NSString stringWithFormat:#"documents://%#",imageName];
NSString *imageSmallName = [NSString stringWithFormat:#"documents://small-%#",imageName];
NSString *caption = [tempDict objectForKey:#"Comment"];
UIImage *tempImage = [UIImage imageWithContentsOfFile:[appDelegate filePath:imageName]];
MockPhoto *tempPicture = [[MockPhoto alloc] initWithURL:fullImageName smallURL:imageSmallName size:tempImage.size caption:caption photoname:imageName];
[photoArray addObject:tempPicture];
Adding photoArray into MockPhotoSource:
self.photoSource = [[MockPhotoSource alloc]
initWithType:MockPhotoSourceNormal
title:#"Photos"
photos:photoArray
photos2:nil
];
[photoArray release];
...But when I give download location like following it doesn't work! Doesn't show me thumbnails and neither large pics!
NSString *url = [delegate downloadImageUrl];
MockPhoto *tempPicture = [[MockPhoto alloc] initWithURL:url smallURL:url size:tempImage.size caption:caption photoname:imageName];
[photoArray addObject:tempPicture];
I have been using same same URL with AsyncImage and it downloads image and show in ImageView fine!
NSURL *url = [NSURL URLWithString:[delegate downloadImageUrl]];
UIImageView *profileimage = [[UIImageView alloc] init];
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:CGRectMake(0, 10, 60, 60)] autorelease];
asyncImage.imageName = [[users objectAtIndex:indexPath.row] objectForKey:#"Key"];
[asyncImage loadImageFromURL:url];
[profileimage addSubview:asyncImage];
Could you please let me know what's wrong am I doing with MockPhoto?
Thanks.

I can't see any picture. Why?

I can't see any picture. Why?
UIImage *photoImg = [UIImage imageNamed:#"http://blog.leadcritic.com/wp-content/uploads/2011/02/favorite_animal_picture.jpg"];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:photoImg];
[self.view addSubview:imageView2];
Because if you use the code:
[UIImage imageNamed:#"mypicture.png"];
The image should be on the iPhone and not loaded from a URL. From a URL you need to use the following:
NSString path = #"http://blog.leadcritic.com/wp-content/uploads/2011/02/favorite_animal_picture.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *photoImg = [[UIImage alloc] initWithData:data cache:NO];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:photoImg];
You haven't set the frame of imageView2
imageView2.frame = CGRectMake( 0, 0, 100, 100 ); // Or whatever.

Xcode iPhone Programming: Loading a jpg into a UIImageView from URL

My app has to load an image from a http server and displaying it into an UIImageView
How can i do that??
I tried this:
NSString *temp = [NSString alloc];
[temp stringwithString:#"http://192.168.1.2x0/pic/LC.jpg"]
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
nil,
(CFStringRef)temp,
NULL,
NULL,
kCFStringEncodingUTF8)
autorelease];
NSData *dato = [NSData alloc];
dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
pic = [pic initWithImage:[UIImage imageWithData:dato]];
This code is in viewdidload of the view but nothing is displayed!
The server is working because i can load xml files from it. but i can't display that image!
I need to load the image programmatically because it has to change depending on the parameter passed!
Thank you in advance.
Antonio
It should be:
NSURL * imageURL = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
Once you have the image variable, you can throw it into a UIImageView via it's image property, ie:
myImageView.image = image;
//OR
[myImageView setImage:image];
If you need to escape special characters in your original string, you can do:
NSString * urlString = [#"http://192.168.1.2x0/pic/LC.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
....
If you're creating your UIImageView programmatically, you do:
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[someOtherView addSubview:myImageView];
[myImageView release];
And because loading image from URL takes ages, it would be better to load it asynchronously. The following guide made it stupid-simple for me:
http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation
Try this
NSURL *url = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]];
[cell addSubview:subview];
[subview release];
All the Best.
This is the actual code.
UIImageView *myview=[[UIImageView alloc]init];
myview.frame = CGRectMake(0, 0, 320, 480);
NSURL *imgURL=[[NSURL alloc]initWithString:#"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];
NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];
UIImage *image=[[UIImage alloc]initWithData:imgdata];
myview.image=image;
[self.view addSubview:myview];
You should load the image in the background or the view will freeze.
Try this:
UIImage *img = [[UIImage alloc] init];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:#"http://www.test.com/test.png"];
img = [UIImage imageWithData: data];
dispatch_async(dispatch_get_main_queue(), ^{
//PUT THE img INTO THE UIImageView (imgView for example)
imgView.image = img;
});
});

setting the images in a tableview which i am getting from the server?

I am using the code below to set images in a table which are loaded from the server, but I have an exception thrown (not every time) which is related to a drawing error
- (void) getTheThumnbails:(NSIndexPath *)indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
GDataEntryYouTubeVideo *entry = [feedArray objectAtIndex:indexPath.row];
NSURL *url = [[NSURL alloc] initWithString:[[[[entry mediaGroup] mediaThumbnails] objectAtIndex:0] URLString]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:imgData];
CGRect newFrame = CGRectMake(0.0, 0.0, 62.0, 62.0);
UIGraphicsBeginImageContext(newFrame.size);
[img drawInRect:newFrame];
UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imgDict setObject:resizedImg forKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[[[myTableView cellForRowAtIndexPath:indexPath] imageView] setImage:resizedImg];
[pool release];
}
Please help me folks, sorting out the mistake i am doing.
Trying to draw a image in a thread is not a good practice, please do not perform any UI operations in thread, they need to be done in the main thread.
well I did the above like
- (void) getTheThumnbails:(NSIndexPath *)indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
GDataEntryYouTubeVideo *entry = [feedArray objectAtIndex:indexPath.row];
NSURL *url = [[NSURL alloc] initWithString:[[[[entry mediaGroup] mediaThumbnails] objectAtIndex:0] URLString]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:imgData];
[imgDict setObject:img forKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[img release];
[self performSelectorOnMainThread:#selector(setImageInTable:) withObject:indexPath waitUntilDone:NO];
[pool release];
}
- (void)setImageInTable:(NSIndexPath *)indexPath{
UIImage *img = (UIImage *)[imgDict valueForKey:[NSString stringWithFormat:#"%i",indexPath.row]];
CGRect newFrame = CGRectMake(0.0, 0.0, 62.0, 62.0);
UIGraphicsBeginImageContext(newFrame.size);
[img drawInRect:newFrame];
UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imgDict removeObjectForKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[imgDict setObject:resizedImg forKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[[[myTableView cellForRowAtIndexPath:indexPath] imageView] setImage:resizedImg];
}
Hope this helps.
Thanks,
Madhup