I am using a method to retrieve the images as a property:
NSData *data = [[NSData alloc] initWithBase64EncodedString:spot.ImageName options:0];
UIImage *image = [UIImage imageWithData:data];
But now I need to retrieve the images from a server "http://IP/MyService/images/1.png" and cash them to my service.
To save the UIImage on file system:
NSString *documentsDirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *imagePath = [documentsDirPath stringByAppendingPathComponent:#"myfile.png"];
NSData *binaryImageData = UIImagePNGRepresentation(imageToSave);
[binaryImageData writeToFile:imagePath atomically:YES];
To retrieve the UIImage on file system:
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];
Related
I was trying to send a UIImage over a simple socket that only accepts CFString for passing data.
I tried converting the UIImage into NSData into NSString into CFString and back, but the final UIImage is always nil.
Conversion:
NSData *dataImage = UIImagePNGRepresentation(image);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Reconversion:
NSString *string = (__bridge NSString *)(object);
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:string options:0];
UIImage *image = [UIImage imageWithData:imageData];
below image After editing the image when i click the share/save button how add images in array. i should be display in tableview and it have save locally.
You can convert a UIImage to NSData like this:
If PNG image
UIImage *image = [UIImage imageNamed:#"imageName.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
If JPG image
UIImage *image = [UIImage imageNamed:#"imageName.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
Add imageData to array:
[self.arr addObject:imageData];
Load images from NSData:
UIImage *img = [UIImage imageWithData:[self.arr objectAtIndex:0]];
Do not store images into an array or a dictionary, unless you want to create a cache, but caches should evict their contents in a future.
You are working on a mobile device that even if is powerful it doesn't has the same hardware of your Mac.
Memory is a limited resource and should be managed with judgment.
If you want to cache temporary those images for performance reason, you can use NSCache, basically is works like mutable dictionary but it is thread safe.
If you want to save them locally it's fine but just keep the path to them in the the array.
- (NSString*) getDocumentsPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths[0]; //Get the document directory
return documentsPath;
}
- (NSString*) writePNGImage: (UIImage*) image withName: (NSString*) imageName {
NSString *filePath = [[self getDocumentsPath] stringByAppendingPathComponent:imageName]; // Add file name
NSData *pngData = UIImagePNGRepresentation(image);
BOOL saved = [pngData writeToFile:filePath atomically:YES]; //Write the file
if (saved) {
return filePath;
}
return nil;
}
- (UIImage*) readImageAtPath:(NSString*) path {
NSData *pngData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:pngData];
return image;
}
I am creating a app in which i am saving some user detail and their messages, This app works similar to Facebook or many other i mean first time data come from api's and then i will store it in our local database. So the data which is loaded once is store in local database and user can see it without internet connection. I have store messages and detail in sqlite database it works fine with text messages and save detail of user.
The Problem is that i want to save profile image with user detail in local storage, i don't want to save it in database but save it into app directory for that i am using this below code
UIImageView *ProfileImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 1, 30, 30)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSData *imageData =[NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"http://ton.hiitap.com/p/%#",[TOUSERUDID objectAtIndex:i]]]];
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#.png",[TOUSERUDID objectAtIndex:i] ]];
[imageData writeToFile:imagePath atomically:YES];
UIImage* img = [UIImage imageWithData:imageData];
NSData *pngData = UIImagePNGRepresentation(img);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-%d.png",[TOUSERUDID objectAtIndex:i],i]]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *img2 = [UIImage imageWithData:pngData];
dispatch_async(dispatch_get_main_queue(),^{
ProfileImage.image=img2;
});
});
The Problem with this code is it only save 2 users profile image and the images which are repeated are not shown when no internet connection is available. Can anyone suggest how to save all users profile images even they are repeated. So that they are shown when no internet connection is available. Thanks in advance.
if you are using core data you can create a column for example userImage with the type NSData in your user model
and you can convert you image to NSData like this :
if PNG image :
UIImage *image = [UIImage imageNamed:#"imageName.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if JPG image :
UIImage *image = [UIImage imageNamed:#"imageName.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
and you can load the image from the database like this :
NSManagedObject *selectedObject = [[self yourFetchCOntroller] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:#"image"]];
// and set this image in to your image View
yourimageView.image=image;
I am making an app that will save a file to the iPhones Documents Directory and then when the View Controller is reloaded it will fetch it from that same location and display it. But it is not fetching it and displaying it.
I am using the following method to save the image:
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
I am trying to recall the image I have saved from the viewDidLoad method with the following code:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *fileName = [NSString stringWithFormat:#"/%#.png", self.fullname.text];
NSString *filePath = [documentDirectoryPath stringByAppendingString:fileName];
[self.imageView setImage:[UIImage imageNamed: filePath]];
However, it is not showing my image. Can anyone help me and tell me where I am going wrong please. I am new to iOS Development.
replace this line: [self.imageView setImage:[UIImage imageNamed: filePath]];
with this one :
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
[self.imageView setImage:image];
You can use [UIImage imageNamed:] with images from the Documents folder and have the benefits of the underlying caching mechanism, but the file path is slightly different.
Use the following:
NSString *fileName = [NSString stringWithFormat:#"../Documents/%#.png", self.fullname.text];
UIImage *image = [UIImage imageNamed:fileName];
You need to use method
- (UIImage*)initWithContentsOfFile:(NSString*)path
to init images from Documents or Cache directories.
imageNamed: is used for getting images from application bundle.
I am storing a UIImage in a NSDictionary, and then passing the NSDictionary at another place in the app. gridImages is a NSMutableArray
NSDictionary *pageDict = [pageArray objectAtIndex:0];
UIImage *pageImage = [pageDict objectForKey:#"image"];
[gridImages addObject:pageImage];
pagedict returns
{
class = is;
image = "<UIImage: 0x14e20e10>";
name = kaka;
page = 64;
}
which presumably I do
UIImage *pageImage = [pageDict objectForKey:#"image"];
However, when I try to add that UIImage to an array, I get this error:
-[UIImage stringByDeletingPathExtension]: unrecognized selector sent to instance 0x14e20e10
Any idea whats going on?
EDIT:
For the NSDictionary, I tried storing the UIImage as NSData:
NSData *imageData = UIImagePNGRepresentation(textbookImage);
[pageDict setObject:imageData forKey:#"image"];
[pageDict setObject:textbookName forKey:#"name"];
[pageDict setObject:page forKey:#"page"];
[pageDict setObject:nameOfClass forKey:#"class"];
And the parsing it like this:
NSData *imageData = [pageDict objectForKey:#"image"];
UIImage *pageImage = [UIImage imageWithData:imageData];
But it still returns the same error
You can't set UIImage into NSUserDefaults as UIImage is non-property-list object.
You better store path of image from documents directory.
This code will help.
// SET Image
NSString *strImagePath = //path of image from documents directory
NSMutableDictionary *dictOne = [NSMutableDictionary dictionaryWithCapacity:0];
[dictOne setObject:strImagePath forKey:#"image"];
[[NSUserDefaults standardUserDefaults] setObject:dictOne forKey:#"dict"];
// GET image
NSMutableDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:#"dict"];
UIImage *image = [UIImage imageWithContentsOfFile:[dict objectForKey#"image"]];
EDIT:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strImageFileName = [documentsDirectory stringByAppendingPathComponent:#"yourImageFileName"];
UIImage *image = // Your image;
NSData *dataImage = UIImageJPEGRepresentation(image, 0.5f);
/* Good image quality
NSData *dataImage = UIImagePNGRepresentation(image);
*/
[dataImage writeToFile:strImageFileName atomically:YES];
Happy coding.