-1

在我的应用程序上,我有一个调用相机的按钮。我想,在用户拍照后,将此图像保存到用户的库中,并将此图像与我的应用程序链接(如 ID)。有一种方法可以做到这一点,而无需将图像保存在我的数据库中?

4

2 回答 2

3

是的,你绝对可以做到!

我最近在 Github 上上传了一个名为“Chanda”的项目,我在其中演示了这种用法。请访问以下 URL 并查看完整代码:

https://github.com/azamsharp/Chanda

这是代码片段,但我强烈建议您下载该项目并查看它的实际效果。

+(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;
}
于 2013-09-16T16:32:57.797 回答
0

我按照本教程将图像的路径保存到数据库:http ://blog.teliaz.com/2010/capture-saveloadremove-image-in-documents-directory 并将图像保存到照片库:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
于 2013-12-10T01:17:59.460 回答