Wednesday, December 8, 2010

Load a image from the server in a ImageVIew

In this post we will see how to load an image in an image view and that image will be present in a different server.

Design Phase: For this application we will just require an object of UIImageView that’s all nothing else and here’s a look at our final output



In case you have forgot what is an image view you can refer my earlier post on UIImageView 

Step 1: Open Xcode and create a window based application and add a UIViewController subclass to your project with the name myviewController now you have added two files with the same name but different extensions that’s myviewController.h and myviewController.m

Step 2: Declare the object of UIImageView in the .h file here’s a view to the code 

@interface myviewController : UIViewController {

UIImageView *myimageView;
}

@end



and in the .m file locate the init method where we will give frames and add the logic of adding an image to the image view from a different server so here’s how the code looks like


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
myimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

//create a NSURL object which will hold the Image URL 
NSURL *imageurl = [NSURL URLWithString:@"http://www.hdwallpapers.in/walls/the_taj_mahal_at_sunset_india-normal.jpg"];
//create a NSData object and init it with the image url path
NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];
//UIImage has a class method called imageWithData supply it with the NSData object and your done
myimageView.image = [UIImage imageWithData:imagedata];
    }
    return self;
}


As you can see I have explained the code with the help of comments so understanding the code should not be a problem for you.

Step 3: Don’t forget to add the image view in the self view with the help of loadView method.

Step 4: Now every thing is set all you have to do now is go to the AppDelegate.m file of your project which is present in the class group and add this view to the iPhone simulator window here’s how will you do that

#import "ImageLoadAppDelegate.h"
#import "myviewController.h"

@implementation ImageLoadAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch

myviewController * myVC = [[myviewController alloc]init];

[window addSubview:myVC.view];

    [window makeKeyAndVisible];
}


Step 5: Press Build and Go and you are done to see the output which looks like this




Save Image to the iPhone gallery



Now lets say you want to save the above image in the iPhone gallery then in that case what you can do is use the below function.

UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector>, void *contextInfo)


Here’s the code which will help you to do this

-(void) SaveImage
{
//The URL of the image
NSString *str = @"http://www.ching-lee.com/blog/wp-content/uploads/2009/08/LINKINPARK.jpg";
//NSURL variable which will store the URL location of the image
NSURL *imageURL = [NSURL URLWithString:str];
NSData *imageData = [[NSData alloc]initWithContentsOfURL:imageURL];
//show the image in the UIImageView
imageView.image = [UIImage imageWithData:imageData];
//Creating the UIImage object and initializing it with the image from a URL
UIImage *tempImage = [[UIImage alloc]initWithData:imageData];
//save the image in the gallery
UIImageWriteToSavedPhotosAlbum(tempImage, nil, nil, nil);
}



In the above code the function saves the image to the user’s Saved photos album. Check out this link for the explanation of the parameters used in the function.

I hope this post was useful for you, comments and queries are always welcome.
Happy iCoding and have a great Day

2 comments:

  1. iPhone herstellingen bij iPhonedokter. Wij zijn een premium iphone reparatie center voor allerlei smartphones zoals iPhone, iPod, iPad, apple, blackberry, Samsung, Nokia, etc. Alle phones herstellen door ons met garantie.

    ReplyDelete
  2. Thank you for this post. It was so useful. Please increase the font because it is so tiny that it is difficult to read the details. Thank you for sharing this though.

    ReplyDelete