2014-02-25 27 views
2

在UIImagePickerController中使用ALBUM時,獲取縮略圖非常容易。使用CAMERA(非專輯)時從UIImagePickerController獲得縮略圖

從VIDEO獲得縮略圖甚至很容易。

但是當你使用相機,

self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera; 

,你回來

-(void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingMediaWithInfo:(NSDictionary *)info 

如何快速獲得縮略圖?

注意,我不是特別想保存圖像。我想使用的圖像從相機(想象一下,然後在繪圖畫面使用它,或者進行圖像處理等)

以下,這是一個天真的類別,將圖像轉換爲128.128圖像。但它太慢了。

許多應用程序很快從相機移動到下一個屏幕(例如,編輯圖像或繪圖)。什麼技術?乾杯

-(UIImage *)squareAndSmall 
{ 
// this category works fine to make a 128.128 thumbnail, 
// but it is very slow 
CGSize finalsize = CGSizeMake(128,128); 

CGFloat scale = MAX(
    finalsize.width/self.size.width, 
    finalsize.height/self.size.height); 
CGFloat width = self.size.width * scale; 
CGFloat height = self.size.height * scale; 

//uses the central area, say.... 
CGRect imageRect = CGRectMake(
    (finalsize.width - width)/2.0f, 
    (finalsize.height - height)/2.0f, 
    width, height); 

UIGraphicsBeginImageContextWithOptions(finalsize, NO, 0); 
[self drawInRect:imageRect]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext(); 
return newImage; 
} 

回答

2

嘗試使用「AssetLibrary.framework」將圖像寫入相機膠捲,它會自動爲您創建縮略圖表示。而且,由於這個過程是異步的,它肯定會提高應用程序的性能。

嘗試一些東西像下面這樣:

#import <AssetsLibrary/AssetsLibrary.h> 

////.........your code 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    /////......... 
    UIImage* img = info[UIImagePickerControllerOriginalImage]; 
    ALAssetsLibrary *library = [ALAssetsLibrary new]; 
    [library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ 
     if (error) { 
      NSLog(@"error"); 
      return; 
     } 

     [library assetForURL:assetURL resultBlock:^(ALAsset *asset) { 
      [self.imageViewer setImage:[UIImage imageWithCGImage:[asset aspectRatioThumbnail]]]; //self.imageViewer is a UIImageView to display thumbnail 
     } failureBlock:^(NSError *error) { 
      NSLog(@"Failed to load captured image."); 
     }]; 
    }]; 
    [library release]; 

    //................ 
} 

////......rest of your code 

我沒有測試的代碼,但我希望,它應該履行你的目的。 請確保在嘗試之前爲您的項目添加「AssetLibrary.framework」參考,並告訴我它是否適合您。 快樂編碼!

+0

順便說一句,Ayan,我沒有保存圖像,也不想保存圖像 - 我只是想在飛行中獲取縮略圖。但是,謝謝你提供的信息豐富的答案!乾杯 – Fattie

+0

@Joe不幸的是資產庫只會在寫作過程中生成縮略圖和任何其他相關圖像('fullResolutionImage' /'fullScreenImage'):( –

+0

優秀信息,謝謝 – Fattie

相關問題