获取相册中最后保存的照片 [英] Get the last saved Photo in photo album

查看:113
本文介绍了获取相册中最后保存的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use AVFoundation framework for taking photo and save it to 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张.

There I used code for getting the last photo and display it on imageView, but it display not last but previous. So if I make 3 photo it will display 2, not 3.

任何想法?

感谢帮助!

推荐答案

您的代码正在执行其应有的工作.您对代码的逻辑有疑问.您要在保存图像完成之前发布通知.您必须在将图像写入库的完成块代码中发布通知.而且,为什么不只在imageView上显示捕获的图像,而不是从库中读取图像呢?

Your code is doing what it's meant to do. You have a issue with the logic of the code. YOu are posting notification before it's finished with saving image. You have to post the notification inside the completion block code of writing image to the library. And, why not just show the captured image on the imageView instead of reading it from the library??

这篇关于获取相册中最后保存的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆