1. 程式人生 > >儲存圖片/視訊到相簿 儲存完成後通知事件 IOS

儲存圖片/視訊到相簿 儲存完成後通知事件 IOS

// 官方提供的說明
//  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
//  - (void)video:(NSString *)videoPath didFinishSavingWithError
:(NSError *)error contextInfo:(void *)contextInfo;
UIKIT_EXTERN void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_1);

---------------------------------------------------------------------------


#pragma mark -
#pragma mark - 儲存圖片或視訊到相簿的通知
@protocol SavedToPhotosAlbumDelegate

@optional

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *) contextInfo; 
- (void)videoSavedToPhotosAlbum:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;


@end

---------------------------------------------------------------------------
// 呼叫儲存的時候直接呼叫這個
UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

// 儲存圖片到相簿
+ (void) writeImageToPhotosAlbumByImage:(UIImage *) v_image andCompletionTarget:(id) v_target andCompletionSelector:(SEL) v_selector andContextInfo:(void*) v_context {
 UIImageWriteToSavedPhotosAlbum(v_image, v_target, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), v_context);
}
// 儲存視訊到相簿
+ (void) writeVideoToPhotosAlbumByVideoPath:(NSString *) v_strVideoPath andCompletionTarget:(id) v_target andCompletionSelector:(SEL) v_selector andContextInfo:(void*) v_context {
 UISaveVideoAtPathToSavedPhotosAlbum(v_strVideoPath, v_target, @selector(videoSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), v_context);
}
---------------------------------------------------------------------------
// 圖片存放到相簿後的通知
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *) contextInfo {  
 NSString *message;  
 NSString *title;  
 if (!error) {  
 title = @"Success";  
 message = @"Save album success!";  
 } else {  
 title = @"Failure";  
#if DEBUG_MODE
 message = [error description];
#else
message = @"Failed to save the album!";
#endif
 }  
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title  
 message:message  
 delegate:nil  
 cancelButtonTitle:@"OK"