相册新增保存gif图片功能

This commit is contained in:
liyuhua
2023-08-29 18:19:57 +08:00
parent 09cbeb72a6
commit 8c21f5117c

View File

@@ -11,8 +11,7 @@
#import "SDBrowserImageView.h"
#import <SDWebImageFLPlugin/SDWebImageFLPlugin.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <PhotosUI/PhotosUI.h>
#import <Photos/Photos.h>
#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO) //iPhoneX
#define iPhoneX_NavSafeArea ([[UIScreen mainScreen] bounds].size.height >= 812.0 ? 24 : 0) // iphoneX 24
@@ -99,18 +98,21 @@
{
int index = _scrollView.contentOffset.x / _scrollView.bounds.size.width;
FLAnimatedImageView *currentImageView = _scrollView.subviews[index];
BOOL isGif = [UIImage isGifWithImageData:currentImageView.animatedImage.data];
if(isGif){
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
NSDictionary *metadate = @{@"UTI":(__bridge NSString *)kUTTypeGIF};
 [library writeImageDataToSavedPhotosAlbum:currentImageView.animatedImage.data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
[self saveImageFinishWithError:error];
}];
NSData *imageData = currentImageView.animatedImage.data;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
if(imageData != nil){
[PHAssetCreationRequest.creationRequestForAsset addResourceWithType:PHAssetResourceTypePhoto data:imageData options:nil];
}else{
UIImageWriteToSavedPhotosAlbum(currentImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
[PHAssetCreationRequest creationRequestForAssetFromImage:currentImageView.image];
}
} completionHandler:^(BOOL success, NSError * _Nullable error) {
//
dispatch_async(dispatch_get_main_queue(), ^{
// 线
[self saveImageFinishWithError:error];
});
}];
if (_indicatorView == nil) {
_indicatorView = [[UIActivityIndicatorView alloc] init];
_indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
@@ -141,28 +143,7 @@
}
[label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
[_indicatorView stopAnimating];
UILabel *label = [[UILabel alloc] init];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
label.layer.cornerRadius = 5;
label.clipsToBounds = YES;
label.bounds = CGRectMake(0, 0, 150, 30);
label.center = self.center;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:17];
[[UIApplication sharedApplication].keyWindow addSubview:label];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
if (error) {
label.text = SDPhotoBrowserSaveImageFailText;
} else {
label.text = SDPhotoBrowserSaveImageSuccessText;
}
[label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
}
- (void)setupScrollView
{