-3

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.

enter image description here

SWAMY CHUNCHU
  • 225
  • 5
  • 14

2 Answers2

2

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]];
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
1

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;
}
Andrea
  • 26,120
  • 10
  • 85
  • 131