iPhone應用程式 將圖片儲存到相簿執行個體

來源:互聯網
上載者:User

iPhone應用程式 將圖片儲存到相簿執行個體是本文要介紹的內容,主要是以代碼來實現本文要表現的內容,進入話題。有時候你的應用需要將應用中的圖片儲存到使用者iPhone或者iTouch的相簿中。 可以使用UIKit的這個類方法來完成。

 
  1. void UIImageWriteToSavedPhotosAlbum (    
  2.    UIImage  *image,    
  3.    id       completionTarget,    
  4.    SEL      completionSelector,    
  5.    void     *contextInfo    
  6. );    
  7. void UIImageWriteToSavedPhotosAlbum (  
  8.    UIImage  *image,  
  9.    id       completionTarget,  
  10.    SEL      completionSelector,  
  11.    void     *contextInfo  
  12. );  

image

要儲存到使用者裝置中的圖片

completionTarget

當儲存完成後,回調方法所在的對象

completionSelector

當儲存完成後,所調用的回調方法。 形式如下:

 
  1. - ( void ) image: ( UIImage *) image  
  2.     didFinishSavingWithError: ( NSError *) error  
  3.     contextInfo: ( void *) contextInfo; 

contextInfo

可選的參數,儲存了一個指向context資料的指標,它將傳遞給回調方法。

比如你可以這樣來寫一個存貯照片的方法:

 
  1. // 要儲存的圖片   
  2.   UIImage *img = [ UIImage imageNamed:@"ImageName.png" ] ;     
  3.    
  4.   // 儲存圖片到相簿中   
  5.   UIImageWriteToSavedPhotosAlbum( img, self, @selector ( image:didFinishSavingWithError:contextInfo:) , nil ) ;  

回調方法看起來可能是這樣:

 
  1. (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error    
  2.              contextInfo:(void *)contextInfo    
  3.   {    
  4.     // Was there an error?     
  5.     if (error != NULL)    
  6.     {    
  7.       // Show error message…     
  8.      
  9.     }    
  10.     else  // No errors     
  11.     {    
  12.       // Show message image successfully saved     
  13.     }    
  14.   }    
  15. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error  
  16.              contextInfo:(void *)contextInfo  
  17.   {  
  18.     // Was there an error?  
  19.     if (error != NULL)  
  20.     {  
  21.       // Show error message…  
  22.    
  23.     }  
  24.     else  // No errors  
  25.     {  
  26.       // Show message image successfully saved  
  27.     }  
  28.   }  

儲存當前視圖:

 
  1. #import  <QuartzCore/QuartzCore.h>   
  2.  
  3. UIGraphicsBeginImageContext(currentView.bounds .size ); //currentView 當前的 view   
  4.  
  5. [currentView. layer  renderInContext: UIGraphicsGetCurrentContext()];   
  6.  
  7. UIImage *viewImage =  UIGraphicsGetImageFromCurrentImageContext();   
  8.  
  9. UIGraphicsEndImageContext();  
  10.  
  11. UIImageWriteToSavedPhotosAlbum(viewImage, nil , nil , nil ); 

小結:iPhone應用程式 將圖片儲存到相簿執行個體的內容介紹完了,希望本文對你有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.