iPhone應用程序 將圖片保存到相冊實例
作者:佚名
本文介紹的是iPhone應用程序 將圖片保存到相冊實例,主要介紹了對圖片的操作。先來看內容。
iPhone應用程序 將圖片保存到相冊實例是本文要介紹的內容,主要是以代碼來實現本文要表現的內容,進入話題。有時候你的應用需要將應用中的圖片保存到用戶iPhone或者iTouch的相冊中。 可以使用UIKit的這個類方法來完成。
- void UIImageWriteToSavedPhotosAlbum (
- UIImage *image,
- id completionTarget,
- SEL completionSelector,
- void *contextInfo
- );
- void UIImageWriteToSavedPhotosAlbum (
- UIImage *image,
- id completionTarget,
- SEL completionSelector,
- void *contextInfo
- );
image
要保存到用戶設備中的圖片
completionTarget
當保存完成后,回調方法所在的對象
completionSelector
當保存完成后,所調用的回調方法。 形式如下:
- - ( void ) image: ( UIImage *) image
- didFinishSavingWithError: ( NSError *) error
- contextInfo: ( void *) contextInfo;
contextInfo
可選的參數,保存了一個指向context數據的指針,它將傳遞給回調方法。
比如你可以這樣來寫一個存貯照片的方法:
- // 要保存的圖片
- UIImage *img = [ UIImage imageNamed:@"ImageName.png" ] ;
- // 保存圖片到相冊中
- UIImageWriteToSavedPhotosAlbum( img, self, @selector ( image:didFinishSavingWithError:contextInfo:) , nil ) ;
回調方法看起來可能是這樣:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
- contextInfo:(void *)contextInfo
- {
- // Was there an error?
- if (error != NULL)
- {
- // Show error message…
- }
- else // No errors
- {
- // Show message image successfully saved
- }
- }
- - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
- contextInfo:(void *)contextInfo
- {
- // Was there an error?
- if (error != NULL)
- {
- // Show error message…
- }
- else // No errors
- {
- // Show message image successfully saved
- }
- }
保存當前視圖:
- #import <QuartzCore/QuartzCore.h>
- UIGraphicsBeginImageContext(currentView.bounds .size ); //currentView 當前的 view
- [currentView. layer renderInContext: UIGraphicsGetCurrentContext()];
- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageWriteToSavedPhotosAlbum(viewImage, nil , nil , nil );
小結:iPhone應用程序 將圖片保存到相冊實例的內容介紹完了,希望本文對你有所幫助!
責任編輯:zhaolei
來源:
互聯網