Use the following functions to get Image from Web URL or Main Bundle (Resource Folder) or Document Directory
// From URL
-(UIImage *) getImageFromURL : (NSString *) urlString
// From Bundle
-(UIImage *) getImageFromResources_FileName: (NSString *)file_Name andFileType: (NSString *) file_Extension
// From Document Directory (NSDocumentDirectory)
-(UIImage *) getImageFromDocumentDirectory_ImageName:(NSString *) imageName
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
// 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];
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];
[alertView show];
}
else
{
else
{
getImage = [UIImage imageNamed:@"defaultImage.png"];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Image" message:@"Not Found" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
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];
Very useful....nice 1
ReplyDelete