UIImageView: Show Images from URL,Resources Folder, and Document Directory

Use the following functions to get Image from Web URL or Main Bundle (Resource Folder) or Document Directory

// From URL

-(UIImage *) getImageFromURL : (NSString *) urlString

{

NSURL *pathURL = [NSURL URLWithString: urlString];

NSData *imageData = [NSData dataWithContentOfURL:pathURL];

UIImage *getImage = [[UIImage alloc]initWithData:imageData];

return getImage;

}

// From Bundle

-(UIImage *) getImageFromResources_FileName: (NSString *)file_Name andFileType: (NSString *) file_Extension

{

NSString *filePath = [[NSBundle mainBundle] pathForResource : file_Name ofType: file_Extension];

UIImage *getImage = [UIImage imageWithContentsOfFile:filePath];

return getImage;

}

// From Document Directory (NSDocumentDirectory)

-(UIImage *) getImageFromDocumentDirectory_ImageName:(NSString *) imageName

{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *destPath = [docDirectory stringByAppendingPathComponent:imageName];//image.png
NSURL *url = [NSURL fileURLWithPath:destPath];
UIImage *getImage= [manager imageWithURL:url];

if (getImage)
{

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@”Image” message:@”Found” delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil];
[alertView show];

}
else
{

getImage = [UIImage imageNamed:@”defaultImage.png”];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@”Image” message:@”Not Found” delegate:nil
cancelButtonTitle:@”OK” otherButtonTitles:nil];
[alertView show];

}

return getImage;

}

Procedure:

#define FROM_RESOURCES 1

#define FROM_WEB_URL 2

#define FROM_DOC_DIR 3

/*******************************************Calling **************************************************/

UIImageView *showImage = [[UIImageView alloc] initWithFrame:CGRectMake(20,20,250,250)];

//Calling from Resources Folder

if(FROM_RESOURCES)

{

showImage.image = [self getImageFromResources_FileName:@”Niketan” andFileType:@”png/jpg”];

}

else if (FROM_WEB_URL)

//Calling from URL

{

showImage.image = [self getImageFromURL : @”your url”];

// @”http://www.yourweb.com/pictures/niketan.png”;

}

else

//Calling From Document Directory

{

showImage.image = [self getImageFromDocumentDirectory_ImageName: @”Vinay.png”];

}

[self.view addSubview: showImage];

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s