保存图片/视频到相册 保存完成后通知事件 IOS

本文详细介绍了如何使用iOS平台上的UIKit框架将图片和视频存储到相册中,并提供了相应的通知机制和保存操作函数。通过实现这些功能,开发者能够确保用户在进行图片和视频分享时的操作体验得到提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 官方提供的说明
//  - (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"  
                                          otherButtonTitles:nil];  
    [alert show];  
    [alert release];  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值