Files
real-e-party-iOS/YuMi/Tools/File/AlbumResourcePickerViewController.m
edwinQQQ a35a711be6 chore: Initial clean commit
- Removed YuMi/Library/ (138 MB, not tracked)
- Removed YuMi/Resources/ (23 MB, not tracked)
- Removed old version assets (566 files, not tracked)
- Excluded Pods/, xcuserdata/ and other build artifacts
- Clean repository optimized for company server deployment
2025-10-09 16:19:14 +08:00

282 lines
11 KiB
Objective-C

//
// AlbumResourcePickerViewController.m
// YuMi
//
// Created by P on 2024/10/18.
//
#import "AlbumResourcePickerViewController.h"
#import <Photos/Photos.h>
#import <SDWebImageFLPlugin/SDWebImageFLPlugin.h>
#import "YYUtility.h"
@interface ImageDetailViewController : UIViewController
@property (nonatomic, strong) UIImage *image; // 用于显示静态图片
@property (nonatomic, strong) NSData *gifData; // 用于显示 GIF 数据
@end
@implementation ImageDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; // 背景颜色为黑色
if (self.gifData) {
// 如果是 GIF 动画
FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:self.gifData];
FLAnimatedImageView *animatedImageView = [[FLAnimatedImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animatedImage = animatedImage;
animatedImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:animatedImageView];
} else if (self.image) {
// 如果是静态图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
[self setupToolArea];
// 添加关闭按钮
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
[closeButton setTitle:@"关闭" forState:UIControlStateNormal];
closeButton.frame = CGRectMake(20, 40, 60, 30);
[closeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(closeButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:closeButton];
}
- (void)setupToolArea {
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 44)];
titleLabel.text = @"选取图片";
// titleLabel.backgroundColor = [UIColor c];
}
// 关闭按钮点击事件
- (void)closeButtonTapped {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
@interface AlbumResourceCollectionCell : UICollectionViewCell
@property (nonatomic, strong) FLAnimatedImageView *imageView;
- (void)setupGifImage:(NSData *)data;
- (void)setupImage:(PHAsset *)asset;
@end
@implementation AlbumResourceCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self.contentView addSubview:self.imageView];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView);
}];
}
return self;
}
- (FLAnimatedImageView *)imageView {
if (!_imageView) {
_imageView = [[FLAnimatedImageView alloc] init];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.clipsToBounds = YES;
}
return _imageView;
}
- (void)setupGifImage:(NSData *)data {
FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];
self.imageView.animatedImage = animatedImage;
}
- (void)setupImage:(PHAsset *)asset {
@kWeakify(self);
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:CGSizeMake(250, 250)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
@kStrongify(self);
self.imageView.image = result;
}];
}
@end
@interface AlbumResourcePickerViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) PHFetchResult *allPhotos;
@property (nonatomic, assign) CGSize cellSize;
@property (nonatomic, assign) CGFloat cellSpace;
@end
@implementation AlbumResourcePickerViewController
+ (void)checkAuthorization:(void(^)(BOOL authorized))result {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (result) {
result(status == PHAuthorizationStatusAuthorized);
}
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self calSize];
[self setupCollectionView];
[self fetchAllPhotos];
}
- (void)calSize {
self.cellSpace = 2;
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat width = (screenWidth - self.cellSpace * self.cellSpace) / 3;
self.cellSize = CGSizeMake(width, width);
}
- (void)fetchAllPhotos {
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
// 获取所有类型的资源
self.allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage
options:fetchOptions];
// 刷新 UICollectionView
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
- (void)handleAsset:(PHAsset *)asset {
// 使用 PHImageManager 来请求图像数据
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
// NSLog(@"%@", info);
if ([dataUTI isEqualToString:@"com.compuserve.gif"]) {
// 是 GIF 文件,处理 GIF 数据
FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
FLAnimatedImageView *animatedImageView = [[FLAnimatedImageView alloc] init];
animatedImageView.animatedImage = animatedImage;
// [self displayAnimatedImageView:animatedImageView];
} else {
// 是普通图片
UIImage *image = [UIImage imageWithData:imageData];
[self displayStaticImage:image];
}
}];
}
- (void)displayAnimatedImageView:(FLAnimatedImageView *)animatedImageView {
animatedImageView.frame = CGRectMake(0, 0, 300, 300); // 设置合适的 frame
[self.view addSubview:animatedImageView];
}
- (void)displayStaticImage:(UIImage *)image {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 300, 300); // 设置合适的 frame
[self.view addSubview:imageView];
}
- (void)setupCollectionView {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = self.cellSpace;
layout.minimumLineSpacing = self.cellSpace;
layout.itemSize = self.cellSize;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[AlbumResourceCollectionCell class] forCellWithReuseIdentifier:@"PhotoCell"];
[self.view addSubview:collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.allPhotos.count; // 返回相册中的资源数量
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AlbumResourceCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
PHAsset *asset = self.allPhotos[indexPath.item];
// 使用 PHImageManager 来请求图像数据,判断是否为 GIF
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
// [cell setupImage:imageData isGif:[dataUTI isEqualToString:@"com.compuserve.gif"]];
if ([dataUTI isEqualToString:@"com.compuserve.gif"]) {
[cell setupGifImage:imageData];
// [cell setupImage:imageData isGif:YES];
} else {
[cell setupImage:asset];
// [cell setupImage:imageData isGif:NO];
// // 是普通静态图片
// [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(100, 100) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
// UIImageView *imageView = [[UIImageView alloc] initWithImage:result];
// imageView.frame = cell.contentView.bounds;
// [cell.contentView addSubview:imageView];
// }];
}
}];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
PHAsset *asset = self.allPhotos[indexPath.item];
// 使用 PHImageManager 来请求图像数据
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
// 创建 ImageDetailViewController 并传递图片数据
ImageDetailViewController *detailVC = [[ImageDetailViewController alloc] init];
if ([dataUTI isEqualToString:@"com.compuserve.gif"]) {
// 是 GIF 文件,传递 GIF 数据
detailVC.gifData = imageData;
} else {
// 是静态图片,传递 UIImage
UIImage *image = [UIImage imageWithData:imageData];
detailVC.image = image;
}
// 弹出全屏显示图片的视图控制器
[self presentViewController:detailVC animated:YES completion:nil];
}];
if (self.didSelectedAsset) {
self.didSelectedAsset(asset);
}
// // 使用 PHImageManager 来请求图像数据
// [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
// if ([dataUTI isEqualToString:@"com.compuserve.gif"]) {
// // 是 GIF 文件,播放 GIF 动画
// FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
// FLAnimatedImageView *animatedImageView = [[FLAnimatedImageView alloc] init];
// animatedImageView.animatedImage = animatedImage;
// [self displayAnimatedImageView:animatedImageView];
// } else {
// // 是静态图片
// UIImage *image = [UIImage imageWithData:imageData];
// [self displayStaticImage:image];
// }
// }];
}
@end