Save image to device - iphone

Hi i cant seem to get his code to work
//Save Image To Device
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.ImageURLS objectAtIndex:0]]]];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(#"saving jpg");
NSString *jpgFilePath = [NSString stringWithFormat:#"%#/%#.jpg",docDir,[self.NameArray objectAtIndex:0]];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpgFilePath atomically:YES];
and i am getting the image via
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *jpgFilePath = [NSString stringWithFormat:#"%#/%#.jpg",docDir,[self.NameArray objectAtIndex:[indexPath row]]];
UIImage *image = [UIImage imageNamed:jpgFilePath];
Any help ?
Thanks

Change the last line of your code to
UIImage *image = [UIImage imageWithContentsOfFile:jpgFilePath];
UIImage's +imageNamed: searches the app bundle for a file with a given name, which is not what you need.

Related

Converting CGImageRef image to image and that UIimage to NSData

I am taking the screenshot of my last viewed Screen, by the following code
//Image Code
UIImage *nextImage;
CGImageRef imgRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
nextImage = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);
CGContextRelease(UIGraphicsGetCurrentContext());
When i displayed the the image in the console i got the O/P like: UIImage: 0x929c760
The Problem lies here for me, When converting that UIImage to NSData i am getting null.
The things i have tried are
//tried
NSData *data = UIImagePNGRepresentation(nextImage);
NSData *data2 = UIImageJPEGRepresentation(nextImage, 1.0);
Hi everyone i have got the solution to my Question as given by the link by #NP Compete
//getting screenshot i haven't used any UIImageview so my code goes as following
-(UIImage*)doImageOperation
{
UIImage *image = [[UIImage alloc]init];
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(#"image in cg %#",image);
[self saveImage:image];
return image;
}
// Saved into Documents Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString:#"test.png"]];
data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
NSData *data = UIImagePNGRepresentation(image1);
NSLog(#"data %#",data);

Not getting images in document directory folder

I want to get images in document directory folder.
I am using this code.
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
for(int i=0;i<[imagepath count];i++)
{
NSString* documentsDirectory = [path objectAtIndex:0];
NSURL *img = [NSURL URLWithString:[imagepath objectAtIndex:i]];
//here imagepath is array in that i get the url of image.
//here when i print the img data i got url from where i have to get images.
NSData *data = [NSData dataWithContentsOfURL:img options:NSDataReadingMapped error:nil];
//here when i put NSLog i get the null.
NSString *fullImagePath1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image%d.png",i]];
[newreturnImage addObject:fullImagePath1];
[data writeToFile:fullImagePath1 options:NSDataWritingAtomic error:nil];
}
I can't get images in document directory folder.
What could be wrong with this code?
Try this code,
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", docDirectory,#"myimage.png"];
UIImage *cellImage=[UIImage imageWithContentsOfFile:filePath];
UIImageView *temp=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
temp.image=cellImage;
[myView addSubview:temp];
[temp release];
Updated :
NSData *myData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
UIImage *myImage = [[UIImage alloc] initWithData:myData];
This link may help you. In the link James Webster has provided an excellent method which suits your requirement. If this does not help you. The code below that would definitely work for you as it works for me.
How save images in home directory?
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
for(int i=0;i<[imagepath count];i++)
{
NSString* documentsDirectory = [path objectAtIndex:0];
NSURL *img = [NSURL URLWithString:[imagepath objectAtIndex:i]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:img];
NSString *fullImagePath1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image%d.png",i]];
[newreturnImage addObject:fullImagePath1];
[data writeToFile:fullImagePath1 options:NSDataWritingAtomic error:nil];
}
Hope this helps you.
Thanks.

How do I save an image to the sandbox - iPhone SDK

I have pulled an image out of the UIImagePicker thingy, and I would like to temporarily save it to the sandbox environment, in either the documents folder, or some other similar equivalent. However, I do not know how to do this, please could someone help me? Thanks
At the moment the image is a UIImage
Saving an image:
-(void)saveImage:(UIImage*)image{
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/someImageName.png"];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}
Loading an image
-(UIImage)loadImage{
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/someImageName.png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
}
-(void)SaveImage
{
NSData *data;
NSArray *paths;
NSString *documentsDirectory,*imagePath ;
UIImage *image=[UIImage imageNamed:#"public.png"];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"public.png"]];
data = UIImagePNGRepresentation(image);
[data writeToFile:imagePath atomically:YES];
}

Iphone : How to Display Document Directory images in Image View?

In a App images stored in Document Directory Folder,
I wanted Particular image display in Image view,
How that image Display in Image View from the Document Directory ?
sample code:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
-(void)loadimage{
NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:#"your image-name"];
UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
[self.view addSubView:myimage];
[myimage release];
}
TNQ
You can achieve it, By knowing the names of your images, so while you are storing your image you need to store some information of these images in database,(or some where you can use it), then fetch name of image from data source, and work as follows:
NSString *str = [NSString stringWithFormat:#"%#.jpg",[myGlobleArrayOfImageNames objectAtIndex:imageCounter]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullImgNm=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:str]];
[mainSlideShowImageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];
Here you can take UIImageView in your View and add it to the view and do the same like as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageFilePath;
NSError *err;
imageFilePath = [documentsDirectory stringByAppendingPathComponent:#"image1.png"];
UIImageView *objImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
objImageView.image = [UIImage imageWithContentsOfFile: imageFilePath];
[self.view addSubVIew:objImageView.image];
[objImageView release];
Please let me know if you still need any help.
NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *imagePath = [directoryPath objectAtIndex:0];
imagePath= [imagePath stringByAppendingPathComponent:#"imagename.png"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *img = = [UIImage imageWithData:data];
you can display this image to uiimageview
you need following lines of code to get UIImage from documents directory
-(UIImage*)getImage:(NSString *)strImageName
{
NSString *documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#",strImageName]];
UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
return image;
}
Call above function and use UIImage object to set anywhere you want.
You can also write this method in a common class to use it anywhere.
Adding to these answers I find its good practice to check if the file exists like so...
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [directory stringByAppendingPathComponent#"my_image.jpg"];
if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
self.myimageview = [UIImage imageWithData:[NSData dataWithContentsOfFile: file]];
}
Simple Use This Code
NSFileManager *filemgr =[NSFileManager defaultManager];
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *dataPath = [docsDir stringByAppendingPathComponent:#"/yourdirectory/image.jpg"];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dataPath]];
self.imageview.image = [[UIImage alloc] initWithData:imgData];

How to store image in big size in local?

I am storing an image locally but my image is store in small size. i want to store this image in orignal size how can i do this ?
my code for store image is like this ..
NSString *myurl = [[NSString alloc]init];
myurl =[data valueForKey:#"large_image"];
NSData *mydata = [[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl]] retain];
UIImage *myimage = [[[UIImage alloc] initWithData:mydata] retain];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *imageName = [NSString stringWithFormat:#"image.png",documentsDirectory];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:Imagename];
[imageData writeToFile:fullPathToFile atomically:YES];
this is stored image
and this is orignal image
i am doing wrong in code. this is the reason. why this happend.
may be it is because of leak ok knowledge that time. :)
other wise all things is correct only.