简体   繁体   中英

Get the last saved Photo in photo album

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM