【问题标题】:How do I get back a saved picture on iPhone camera roll?如何取回 iPhone 相机胶卷上保存的照片?
【发布时间】:2014-04-21 03:19:04
【问题描述】:

我是 iOS 开发的新手,我正在尝试创建一个允许用户使用相机拍照的视图。

我想要发生的是当用户拍摄照片时,它将照片保存到他们的相机胶卷中。我相信可以通过以下语句简单地完成:

//Let's say the image you want to save is in a UIImage called "imageToBeSaved"
UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);

但是,我的问题是,如果用户离开视图然后返回视图,我仍然希望该图片存在。那么如何取回我之前拍摄/保存的同一张照片,以便在他们重新打开视图时重新加载它?

任何帮助都会很棒!谢谢。

【问题讨论】:

  • 您可以使用 imagepicker 从相册中获取该图像。或者以编程方式,您可以从相册中获取最后捕获的图​​像
  • 如何使用 imagepicker 抓取特定图像?每张图片保存的时候都有id吗?
  • 其实你可以把图片保存到Documents目录,也可以从这个目录读取。这是一个持久的解决方案,因为 Documents 目录位于您的应用程序包中,没有其他应用程序可以访问它。

标签: ios iphone objective-c ipad ios7


【解决方案1】:

此代码 sn-p 将从相机胶卷中获取最新图像:reference link

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    // Chooses the photo at the last index
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

        // The end of the enumeration is signaled by asset == nil.
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

            // Stop the enumerations
            *stop = YES; *innerStop = YES;

            // Do something interesting with the AV asset.
            [self sendTweet:latestPhoto];
        }
    }];
} failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
}];

【讨论】:

  • 我希望它每次都能抓取相同的特定图像。如果用户用他们的相机或其他应用程序拍照,这段代码是否会检索该图像,因为我的不再是最后一张拍摄的图像?
  • 你可以用名字保存图片,并从相册中获取该特定图片。这可能是一个解决方案。
  • 我会建议您将应用中的图片保存到自定义相册中,并从相册中获取最后一张图片。
  • 这听起来是个好主意。如何创建自定义相册?
【解决方案2】:
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectoryPath = dirPaths[0];
    NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];

    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:filePath atomically:YES];
}

void UIImageReadFromFile(UIImage **image, NSString *fileName)
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectoryPath = dirPaths[0];
    NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];

    image = [UIImage imageWithContentsOfFile:filePath];
}

图像将被保存到您的应用程序包的 Documents 目录并从中读取指定名称。

使用示例:

UIImageWriteToFile(image, @"somephoto.png");

UIImage *fetchedImage;
UIImageReadFromFile(&fetchedImage, @"somephoto.png");

【讨论】:

  • 非常感谢!这就是我一直在寻找的。真的很感激:)
  • 如何将变量解析为文件名?我似乎无法让它工作。我有以下内容: UIImageWriteToFile(self.imageView.image, @"%@.png", self.fullname.text);
猜你喜欢
  • 1970-01-01
  • 2011-11-26
  • 2010-12-29
  • 2013-04-03
  • 2014-09-04
  • 2012-07-22
  • 2013-07-18
  • 1970-01-01
  • 2012-05-01
相关资源
最近更新 更多