将图像添加到iPhone应用程序中

时间:2011-06-29 09:31:23

标签: iphone camera

我有一个应用程序,用户应该可以使用相机拍照并将其保存在应用程序中。默认情况下,所有拍摄的图像都保存到相册中。但我想将它们带入我的项目,就像我们将项目所需的图像添加到Xcode的Resources文件夹中一样。同时我希望它们转换成PNG格式。

3 个答案:

答案 0 :(得分:1)

从相机拍摄图像后,您可以将此图像保存到应用程序的文档目录中,

UIImage *image = imageFromCamera;
    NSData *imageData = UIImagePNGRepresentation(image);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:@"MyImage.png"];
    [imageData writeToFile:fullPathToFile atomically:YES];

它经过测试的代码并且工作得非常好。

答案 1 :(得分:0)

这是一个很棒的教程,您应该查看http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html

以下是来自Apple的UIImagePickerController的文档,你应该使用https://developer.apple.com/documentation/uikit/uiimagepickercontroller

答案 2 :(得分:0)

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

    NSString *path = [DocsDir stringByAppendingPathComponent:@"image.jpg"];

//Check if image exists on documents path, if exists removed it, but it depends on your logic, you can add more images in documents path by changing its name...
    BOOL success = [FileManager fileExistsAtPath:zipPath];

    if(success){
        [FileManager removeItemAtPath:path error:&error];
    }

    [UIImageJPEGRepresentation(image,1.0) writeToFile:path atomically:YES];