-1

On my app, I have a button that is calling the Camera. I want to, after user takes the picture, save this image to the user's library and link this image with my app (like an ID). There is a way to do this without saving the image on my databae?

Lücks
  • 3,806
  • 2
  • 40
  • 54
  • Do you mean you need to refer an image in iOS asset library without copying? You can do the way azamsharp proposed but in this case you still need to save image to app's Documents directory (I'm not sure what do you mean with database). – Misha Karpenko Sep 16 '13 at 16:39
  • Actually, I am also saving the image ID to the database SQLite using FBDB so I can later refer to it and retrieve the image. – azamsharp Sep 16 '13 at 17:39

2 Answers2

3

Yes absolutely you can do that!

I recently uploaded a project on Github called "Chanda" where I demonstrate that usage. Please visit the following URL and check out the complete code:

https://github.com/azamsharp/Chanda

Here is the snippet of code but I highly recommend that you download the project and see it in action.

+(NSString *) getUniqueIdentifier
{
    CFUUIDRef uuid;
    CFStringRef uuidStr;

    uuid = CFUUIDCreate(NULL);
    uuidStr = CFUUIDCreateString(NULL, uuid);

    return (__bridge NSString *)(uuidStr);
}

// user generated images are stored in the documents directory
+(UIImage *) loadImageFromDocumentsDirectory:(NSString *)imageUrl
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageUrl]];

    UIImage *image = [UIImage imageWithContentsOfFile:savedImagePath];

    return image;
}

// save user's image into the documents directory
+(NSString *) saveImageIntoDocumentsDirectoryAndReturnPath:(UIImage *)imageToSave
{
    imageToSave = [imageToSave imageByScalingProportionallyToSize:CGSizeMake(320, 480)];

    NSString *uniqueImageName = [self getUniqueIdentifier];

    uniqueImageName = [uniqueImageName stringByAppendingPathExtension:@"png"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",uniqueImageName]];
    NSData *imageData = UIImagePNGRepresentation(imageToSave);

    [imageData writeToFile:savedImagePath atomically:YES];

    return uniqueImageName;
}
azamsharp
  • 19,710
  • 36
  • 144
  • 222
0

I saved the image's path to database, following this tutorial: http://blog.teliaz.com/2010/capture-saveloadremove-image-in-documents-directory And save the image to the photo library:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Lücks
  • 3,806
  • 2
  • 40
  • 54