Get the last saved Photo in photo album

落花浮王杯 提交于 2019-12-12 04:47:48

问题


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];

                                                               }

                                                           }];
}

I postNotification to the method 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");
    }];
  }

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.

Any ideas???

Thanks for help!


回答1:


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??



来源:https://stackoverflow.com/questions/18372408/get-the-last-saved-photo-in-photo-album

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!