I try to set an image for a UIButton from documents directory
i tried this:
-(IBAction) vignettesBank1:(id)sender {
NSString *localPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *imageBouton1 = [localPath stringByAppendingPathComponent:[[NSString alloc]initWithFormat:#"btn01.png"]];
[boutonVideo1 setImage:[UIImage imageNamed:imageBouton1] forState:UIControlStateNormal];
NSLog(#"imageBouton1=%#",imageBouton1);}
log return :
imageBouton1=/var/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png
i try this too :
-(IBAction) vignettesBank1:(id)sender {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *pngfile = [documentsDir stringByAppendingPathComponent:#"btn01.png"];
[boutonVideo1 setImage:[UIImage imageNamed:pngfile] forState:UIControlStateNormal];
NSLog(#"imageBouton1=%#",pngfile);}
log return the same :
imageBouton1=/var/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png
my button doesn't load my image
thank's for your help
imageNamed: is for files in you bundle. You should use
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath: imageBouton1]];
[boutonVideo1 setImage:[[UIImage alloc] initWithData:pngfile] forState:UIControlStateNormal];
Related
I am using following code for getting single image. But, how to do it for multiple images?
This is my code :
-(void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorInBackground:#selector(loadImageIntoMemory) withObject:nil];
}
-(void)loadImageIntoMemory {
NSLog(#"Beginning download");
NSString *temp_Image_String = [[NSString alloc] initWithFormat:#"http://www.thewavestore.com/appproductimage/"];
NSURL *url_For_Ad_Image = [[NSURL alloc] initWithString:temp_Image_String];
NSData *data_For_Ad_Image = [[NSData alloc] initWithContentsOfURL:url_For_Ad_Image];
UIImage *temp_Ad_Image = [[UIImage alloc] initWithData:data_For_Ad_Image];
[self saveImage:temp_Ad_Image];
UIImageView *imageViewForAdImages = [[UIImageView alloc] init];
imageViewForAdImages.frame = CGRectMake(0, 0, 320, 480);
imageView.image = [self loadImage];
[self.view addSubview:imageView];
}
-(void)saveImage:(UIImage *)image {
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: #"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
-(UIImage *)loadImage {
NSLog(#"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Please help to over come from this issues,if any good idea please suggest me.
First save your images with unique names (i am using current time) and save this names to an array
NSData *imageData = UIImagePNGRepresentation(chosenImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
// save image with currentdate and time in the name
NSString * imageName = [NSString stringWithFormat:#"name_%#.png",[dateFormatter stringFromDate:[NSDate date]]];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
//add all the images names to the imagepath array
[imagePathArray addObject:imageName];
//write image into file
[imageData writeToFile:fullPathToFile atomically:YES];
then retrive images from image path array
for (NSString*path in imagePathArray ) {
NSArray *filepart = [path componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/%#",
documentsDirectory,path];
//NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
}
1. Store the server images in to array.
NsMutableArray *arrImages=[[nsmutableArray alloc]init];
2.Get the count of the array to run this in for loop.
for(int i=0;i<[arrImages count];i++)
{
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: arrImages[i]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
To convert it to JPEG
NSData=UIImageJPEGRepresentation(UIImage,1.0);
I want to save and load image in UIImageView
I have already tried some code but it doesn't work.
The Code i have tried:
Save:
UIImage *image = ...;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
Load:
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
But these code doesn't work!
I hope you want to save image in document directory folder
here is the way to save image:
- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: #"test.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
to get image from document directory:
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: #"test.png"] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Hope this will help
Globally declare imageView
UIImageView *imageView;//if u create imageView in interfaceBuilder. then u can replace this line by creating outlet of ur imageView
for loading image:
//Loading image in imageView
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachedImagePath])
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
if (image)
{
imageView = [[UIImageView alloc]init];//Dont do this, if u created imageView using interFace builder
[imageView setFrame:CGRectMake(50, 100, 200, 200)];//Dont do this, if u created imageView using interFace builder
[self.view addSubview:imageView];//Dont do this, if u created imageView using interFace builder
[imageView setImage:image];
}
else
{
NSLog(#"Unsupported File");
}
}
else
{
NSLog(#"Image not Found in the path");
}
for saving image:
UIImage *image = imageView.image;
if (image)
{
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"imageSaved.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachedImagePath])
{
NSLog(#"File Created");
}
}
else
{
NSLog(#"No image to save");
}
I have .png images in the Documents folder of my app but when i try to load them it don't work :/
I have this class for the methods for saving and loading :
UIImage+loadScan.h :
#import <UIKit/UIKit.h>
#interface UIImage (loadScan)
- (void)saveScan:(NSString*)name;
- (UIImage *)loadScan:(NSString*)name;
#end
.m :
#import "UIImage+loadScan.h"
#implementation UIImage (loadScan)
#pragma SAVE AND LOAD
- (void)saveScan:(NSString*)name{
if (self != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString:name] ];
NSData* data = UIImagePNGRepresentation(self);
[data writeToFile:path atomically:YES];
}
}
- (UIImage *)loadScan:(NSString*)name{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [NSString stringWithFormat:#"%#/%#", documentsDirectory, name];
NSLog(#"loading %#", path);
NSData *imgData = [NSData dataWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:imgData];
//UIImage *image = [UIImage imageWithContentsOfFile:path];
return image;
}
#end
The method saveScan work good :
[image saveScan:[NSString stringWithFormat:#"%#.png", imgName]];
But loadScan don't want to work :
UIImage *imageLoad = [[UIImage alloc] init];
[imageLoad loadScan:[NSString stringWithFormat:#"%#.png", imgName]];
cell.imageScan.image = imageLoad;
I don't understand why, yet the file exist.
Thanks
I have found a (strange) method, but it work (i send the UIImage also un parametter and init it in the method) :
- (UIImage *)loadScan:(NSString*)name theImage:(UIImage*)theImg{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [NSString stringWithFormat:#"%#/%#", documentsDirectory, name];
NSLog(#"loading %#", path);
[theImg initWithContentsOfFile:path];
return theImg;
}
Call with :
UIImage *imageLoad = [UIImage alloc];
[imageLoad loadScan:[NSString stringWithFormat:#"%##2x.png", imgName] theImage:imageLoad];
Now imageLoad have the image data and we can show it !
I have image file which is stored in the iphone documents dir when i gave that image to button background it does not show.
NSString*imageThumbnailVideo= [NSString stringWithFormat:#"%#thumbVideo.png",imageThumbnail];
NSLog(#"imageThumbnailVide is %#",imageThumbnailVideo);
NSString * filePath = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(),imageThumbnailVideo];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists == 0)
{
NSLog(#"File is Not exists");
}
else
{
NSLog(#"File is present");
}
[thumbnailImageView setBackgroundImage:[UIImage imageNamed:imageThumbnailVideo] forState:UIControlStateNormal];
It also shows File is Present but does not display image.
Here is how i am saving the file
testT=[NSString stringWithFormat:#"%#thumbVideo",titleTextField.text];
NSString *newStringVT = [testT stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString*imageTitleVT=[NSString stringWithFormat:#"%#.png",newStringVT];
NSString *pathVT = [documentsDirectory stringByAppendingPathComponent:imageTitleVT];
NSData*thumbVideoData=UIImagePNGRepresentation(thumbnail);
NSError * error = nil;
[thumbVideoData writeToFile:pathVT options:NSDataWritingAtomic error:&error];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *img_path = [documentsDirectory stringByAppendingPathComponent:#"YOUR IMAGE NAME.png"];
NSURL* uurl=[NSURL fileURLWithPath:img_path];
[thumbnailImageView setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:uurl]] forState:UIControlStateNormal];
You can obtain the image from a file by doing something like this:
UIImage* anImage = [UIImage imageWithContentsOfFile:validFilePath];
and then you can apply that image to a button like this:
[_someButton setImage:anImage forState:UIControlStateNormal];
I am fetching array of images from different urls..the images are loading perfectly..but if i need to display the images in offline mode..I cant find a right way to get it.Here is my code
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *arr = [NSArray arrayWithObjects:[NSURL URLWithString:
#"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
[NSURL URLWithString:#"http://challenge.roadef.org/2012/img/google_logo.jpg"],
[NSURL URLWithString:#"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
[NSURL URLWithString:#"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
[NSURL URLWithString:#"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
[NSURL URLWithString:#"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg"],Nil];
NSURL *url=[NSURL URLWithString:arr];
NSData* theData = [NSData dataWithContentsOfURL:url];
int row = 0;
int column = 0;
for(int i = 0; i < [(NSArray*) url count]; ++i) {
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*) [(NSArray*)url objectAtIndex:i]]];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+16, row*90, 80, 80);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:arr];
[theData writeToFile:localFilePath atomically:YES];
}
Guidance please...
Try SDWebImage, SDWebImage can download image and save into disk for cache, that you can use in offline.
You should be saving the images to your device, so you can access them when you're not connected to the internet. The way you do it right now you always load the url.
You can try it like this:
Use a NSData object for every image and load the url content in it.
NSData* theData = [NSData dataWithContentsOfURL:yourURLHere];
Now you can save this object to your device and access it whenever you need to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"picName.jpg"];
[theData writeToFile:localFilePath atomically:YES];
And that should be about it