Link a picture taken on my app [closed]

一曲冷凌霜 提交于 2019-12-13 08:58:41

问题


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?


回答1:


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;
}



回答2:


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);


来源:https://stackoverflow.com/questions/18832853/link-a-picture-taken-on-my-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!