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.
How to add images in Array ios?
2.8k Views Asked by SWAMY CHUNCHU At
2
There are 2 best solutions below
0
On
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;
}

You can convert a UIImage to NSData like this:
If PNG image
If JPG image
Add imageData to array:
Load images from NSData: