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.

<strong>Any ideas???</strong>

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


  • How to call UIImagePickerController Delegate programmatically or forcefully [without user interactio
  • Get timestamp of images in Photo Library using ALAssetLibrary
  • How to change int value in block?
  • How to get photos from camera roll?
  • Using AFNetworking to POST a file asynchronously with upload progress to Strava V3 API
  • way to start activity indicator on click event of button before its touch ends?
  • Upload canvas element to webserver/database?
  • MFMailComposer not working in io6 landscape mode
  • Paper.js Loading Images and the Active Layer
  • How to receive ALAssetsLibraryChangedNotification in iOS 4 while in background
  • Yelp API ios getting a larger image
  • Convert Tiff data type in libTiff to nsdata ios
  • Deleting in NSDocumentDirectory
  • How create UIImage from bytes?
  • winrt, javascript, image from byte array
  • Error Domain=NSCocoaErrorDomain Code=3840 “Invalid value around character 0.” UserInfo={NSDebugDescr
  • include dlls in visual studio c++ 2008
  • what does prefix @- mean in makefile?
  • Does Apportable support to build library binary (.a/.so)?
  • Why does java tzupdater add leap seconds?
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • How to access culture data in globalize.js V1.0.0
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • How to generate and display a QR Code in ionic 2
  • What's the purpose of QString?
  • Connect .sks to skscene.h
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Xcode 4 NSLog Macro link in Xcode 3
  • Hardware Accelerated Image Scaling in windows using C++
  • debug library loaded with ctypes using gdb
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Why winpcap requires both .lib and .dll to run?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?