相册新增保存gif图片功能

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

View File

@@ -11,12 +11,11 @@
#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) {
NSData *imageData = currentImageView.animatedImage.data;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
if(imageData != nil){
[PHAssetCreationRequest.creationRequestForAsset addResourceWithType:PHAssetResourceTypePhoto data:imageData options:nil];
}else{
[PHAssetCreationRequest creationRequestForAssetFromImage:currentImageView.image];
}
} completionHandler:^(BOOL success, NSError * _Nullable error) {
//
dispatch_async(dispatch_get_main_queue(), ^{
// 线
[self saveImageFinishWithError:error];
}];
}else{
UIImageWriteToSavedPhotosAlbum(currentImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
});
}];
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
{
@@ -176,7 +157,7 @@
for (int i = 0; i < self.imageCount; i++) {
SDBrowserImageView *imageView = [[SDBrowserImageView alloc] init];
imageView.tag = i;
//
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoClick:)];
@@ -231,7 +212,7 @@
}
}
CGRect targetTemp = (!CGRectEqualToRect(self.sourceImagesFrame, CGRectZero)) ? self.sourceImagesFrame : [self.sourceImagesContainerView convertRect:sourceView.frame toView:self];
CGRect targetTemp = (!CGRectEqualToRect(self.sourceImagesFrame, CGRectZero)) ? self.sourceImagesFrame : [self.sourceImagesContainerView convertRect:sourceView.frame toView:self];
UIImageView *tempView = [[UIImageView alloc] init];
tempView.contentMode = sourceView.contentMode;
tempView.clipsToBounds = YES;
@@ -246,7 +227,7 @@
tempView.center = self.center;
[self addSubview:tempView];
_saveButton.hidden = YES;
[UIView animateWithDuration:SDPhotoBrowserHideImageAnimationDuration animations:^{
@@ -269,7 +250,7 @@
}
SDBrowserImageView *view = (SDBrowserImageView *)recognizer.view;
[view doubleTapToZommWithScale:scale];
}
@@ -337,7 +318,7 @@
sourceView = [view cellForItemAtIndexPath:path];
} else {
NSIndexPath *path = [NSIndexPath indexPathForItem:self.currentImageIndex inSection:0];
sourceView = [view cellForItemAtIndexPath:path];
sourceView = [view cellForItemAtIndexPath:path];
}
}else {
if (self.currentImageIndex<self.sourceImagesContainerView.subviews.count) {