0

I have written an app that allows the user to take photos which are stored in an NSArray and which are then saved in the Documents directory. When they are reloaded the pictures are stored in an UITableView - note only one photo is currently loaded due to problem 1.

I have two problems:

1 - This works but only for one photo as I need to be able work out how many photos have been stored.

2 - When I reload the data and put into an UIImageView the rotation of the photos is incorrect. Landscape photos are upside down and portrait photos are landscape.

Any help with either of these issues greatly appreciated.

Saving the photos

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

for (int i=0;i<photoCount;i++)
{
    UIImage *image = [photos objectAtIndex:i];
    NSString *savePicturePath = [saveFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image_%d",i]];
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savePicturePath atomically:NO];
}

Retrieving the photos

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getPicPath = [documentsDirectory stringByAppendingPathComponent:@"image_0"];
NSData *picData = [[NSFileManager defaultManager] contentsAtPath:getPicPath];

UIImage *loadedImage = [UIImage imageWithData:picData];
[photos addObject:loadedImage];
[self.tableView reloadData];

Adding image to table

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"photoCell"];
UIImage *currentImage = [self.photos objectAtIndex:indexPath.row];
cell.imageView.image = currentImage;
return cell;
}
RGriffiths
  • 5,722
  • 18
  • 72
  • 120

2 Answers2

1

You could solve the first issue by creating your own directory and storing all images to that directory. Then when you read use NSFileManager's contentsOfDirectoryAtPath to get an array of all the images in that directory. Or you could read the content of the documents folder and use NSPredicate to filter out only images by file extension (using filteredArrayUsingPredicate).

As for the rotation issue, the answer is available here.

Community
  • 1
  • 1
Joel
  • 15,654
  • 5
  • 37
  • 60
  • Thanks. I fact I do create a folder for the images but I removed the code here to keep it from being too lengthy. I will look into contentsOfDirectoryAtPath - am I right in thinking it returns an array of the directory contents? The other answer explains things very well - thanks so much. – RGriffiths Nov 19 '13 at 06:41
0

The rotation issue ... simple:

Change

NSData *imageData = UIImagePNGRepresentation(image);

to

NSData *imageData = UIImageJPGRepresentation(image, 1.0);

And everything is perfect. Thanks to Joel for pointing me in the right direction.

RGriffiths
  • 5,722
  • 18
  • 72
  • 120