将图片直接从iPhone相机保存到文档文件夹而不是相机胶卷?

3

我正在尝试找出如何使用iPhone相机拍摄并直接将图像保存到应用程序的文档文件夹中,如果可能的话不要保存到相机胶卷。

我的应用程序可以显示相机,并且我可以将图像保存到相机胶卷中,但我想直接保存到应用程序的文档文件夹中。

有什么建议吗?

感谢您的帮助。


这个 Storepath 是什么?它是预定义的函数还是我们需要提供路径?如果需要提供路径,那么应该怎么做? - MQ.
1个回答

5

你可以尝试在你的- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法中这样做:

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(pickedImage);

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"imageName.png"];

NSError * error = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&error];

if (error != nil) {
    NSLog(@"Error: %@", error);
    return;
}

如果这个对您有用,请告诉我。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接