ios - 获取相册中最后保存的照片

标签 ios objective-c avfoundation alassetslibrary

我使用 AVFoundation 框架拍照并将其保存到 photoAlbum:

- (void)captureStillImage
{  
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { 
      break; 
    }
    }if ([videoConnection isVideoOrientationSupported])
        [videoConnection setVideoOrientation:self.orientation];

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
                                                           {
                                                               ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
                                                                   if (error) {
                                                                       NSLog(@"ERROR!!!");
                                                                   }
                                                               };

                                                               if (imageDataSampleBuffer != NULL)
                                                               {                                                                   

                                                                   NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                                   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

                                                                   UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                                   [library writeImageToSavedPhotosAlbum:[image CGImage]
                                                                                             orientation:(ALAssetOrientation)[image imageOrientation]
                                                                                         completionBlock:completionBlock];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                                                               }

                                                           }];
}

我将通知发布到方法saveImageToPhotoAlbum:

- (void)saveImageToPhotoAlbum
{   
    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 enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets] - 1] options:0 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]];

                self.imageView.image = latestPhoto;
            }
        }];
    } failureBlock: ^(NSError *error) {
        NSLog(@"No groups");
    }];
  }

我使用代码获取最后一张照片并将其显示在 imageView 上,但它显示的不是最后一张而是上一张。因此,如果我制作 3 张照片,它将显示 2,而不是 3。

有什么想法吗???

感谢您的帮助!

最佳答案

您的代码正在做它应该做的事情。您对代码的逻辑有疑问。您在完成保存图像之前发布通知。您必须在将图像写入库的完成 block 代码中发布通知。而且,为什么不直接在 imageView 上显示捕获的图像而不是从库中读取它呢??

关于ios - 获取相册中最后保存的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18372408/

相关文章:

objective-c - 仅资源 NSBundle : is this kosher?

ios - 不同的AVAudioQuality选项对生成的录音有什么影响?

iphone - 使用 ios Camera 以方形形式聚焦到对象?

ios - 异步加载 - UITableView 和 Firebase

ios - 安装的应用程序每次启动时都会崩溃

ios - 当我运行我的项目时,搜索栏不会出现在 UICollectionView 的标题中

objective-c - 如何以编程方式隐藏Mac应用程序中的所有应用程序?

ios - ABPersonViewController 不会关闭键盘

ios - 查看 iOS 8 上已安装的扩展

ios - 如何在 iOS 11 中正确停止和删除音频处理器