2013-08-22 40 views
0

我用AVFoundation框架拍照並保存到畫冊:獲取最後保存的照片在相冊

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

                   } 

                  }]; 
} 

我postNotification的方法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.

任何想法?

感謝您的幫助!

+0

我認爲索引是從0開始的。所以嘗試檢查其他東西,比如4,5 .....然後你會得到想法 – SRI

回答

2

您的代碼正在做它意味着什麼。您對代碼的邏輯有問題。 YOu在完成保存圖像之前發佈通知。您必須在將圖像寫入庫的完成塊代碼中發佈通知。而且,爲什麼不直接在imageView上顯示捕獲的圖像而不是從庫中讀取?

+0

樂意幫忙:)改變圖像方向的最好和簡單的方法是:'newImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:1。取向:(UIImageOrientationRight)];'。只要改變方位參數,你就可以走了;) –

相關問題