iPhone應(yīng)用程序 將圖片保存到相冊(cè)實(shí)例
iPhone應(yīng)用程序 將圖片保存到相冊(cè)實(shí)例是本文要介紹的內(nèi)容,主要是以代碼來實(shí)現(xiàn)本文要表現(xiàn)的內(nèi)容,進(jìn)入話題。有時(shí)候你的應(yīng)用需要將應(yīng)用中的圖片保存到用戶iPhone或者iTouch的相冊(cè)中。 可以使用UIKit的這個(gè)類方法來完成。
- void UIImageWriteToSavedPhotosAlbum (
- UIImage *image,
- id completionTarget,
- SEL completionSelector,
- void *contextInfo
- );
- void UIImageWriteToSavedPhotosAlbum (
- UIImage *image,
- id completionTarget,
- SEL completionSelector,
- void *contextInfo
- );
image
要保存到用戶設(shè)備中的圖片
completionTarget
當(dāng)保存完成后,回調(diào)方法所在的對(duì)象
completionSelector
當(dāng)保存完成后,所調(diào)用的回調(diào)方法。 形式如下:
- - ( void ) image: ( UIImage *) image
- didFinishSavingWithError: ( NSError *) error
- contextInfo: ( void *) contextInfo;
contextInfo
可選的參數(shù),保存了一個(gè)指向context數(shù)據(jù)的指針,它將傳遞給回調(diào)方法。
比如你可以這樣來寫一個(gè)存貯照片的方法:
- // 要保存的圖片
- UIImage *img = [ UIImage imageNamed:@"ImageName.png" ] ;
- // 保存圖片到相冊(cè)中
- UIImageWriteToSavedPhotosAlbum( img, self, @selector ( image:didFinishSavingWithError:contextInfo:) , nil ) ;
回調(diào)方法看起來可能是這樣:
- (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
- }
- }
保存當(dāng)前視圖:
- #import <QuartzCore/QuartzCore.h>
- UIGraphicsBeginImageContext(currentView.bounds .size ); //currentView 當(dāng)前的 view
- [currentView. layer renderInContext: UIGraphicsGetCurrentContext()];
- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageWriteToSavedPhotosAlbum(viewImage, nil , nil , nil );
小結(jié):iPhone應(yīng)用程序 將圖片保存到相冊(cè)實(shí)例的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!


















