前言
現(xiàn)在做項(xiàng)目要新加gif的上傳泣港,顯示和保存到相冊(cè),之前以為挺簡(jiǎn)單的就改個(gè)后綴历葛,??????
gif的上傳
1.從相冊(cè)獲取gif圖片
先判斷獲取的圖片中有沒有g(shù)if進(jìn)行不同的業(yè)務(wù)邏輯處理正塌,比如圖片會(huì)加濾鏡之類的
[WJTZImageManager showGifImagePhotoWithController:self count:9 finishBlock:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if ([WJTZImageManager haveGifWithAssetArray:assets]) {
[myself gotoSayPublishControllerWithImageArray:photos assetArray:assets type:MouoPublishSayType];
}else {
[myself gotoFilterControllerWithImageArray:photos assetArray:assets type:MouoPublishSayType];
}
}];
2.NSData數(shù)據(jù)上傳
1.系統(tǒng)自帶的UIImage里面轉(zhuǎn)成NSData類型就不是圖了所以我們要用以下的轉(zhuǎn)換方法
- (void)getOriginalPhotoDataWithAsset:(id)asset completion:(void (^)(NSData *data,NSDictionary *info,BOOL isDegraded))completion {
if ([asset isKindOfClass:[PHAsset class]]) {
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.networkAccessAllowed = YES;
option.resizeMode = PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]);
if (downloadFinined && imageData) {
if (completion) completion(imageData,info,NO);
}
}];
} else if ([asset isKindOfClass:[ALAsset class]]) {
ALAsset *alAsset = (ALAsset *)asset;
ALAssetRepresentation *assetRep = [alAsset defaultRepresentation];
Byte *imageBuffer = (Byte *)malloc(assetRep.size);
NSUInteger bufferSize = [assetRep getBytes:imageBuffer fromOffset:0.0 length:assetRep.size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
if (completion) completion(imageData,nil,NO);
}
}
2.由于是異步的,需要線程同步恤溶,我這些了一個(gè)方法
+ (void)getDataWithArray:(NSArray *)array finish:(void(^)(NSArray *array))finish {
NSMutableArray *dataArray = [NSMutableArray array];
NSMutableArray *assetArray = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[UIImage class]]) {
[dataArray addObject:UIImageJPEGRepresentation(obj, 0.8)];
}
if ([obj isKindOfClass:[NSData class]]) {
[dataArray addObject:obj];
}
if ([obj isKindOfClass:[PHAsset class]]) {
[assetArray addObject:obj];
}
}];
dispatch_group_t group = dispatch_group_create();
[assetArray enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
dispatch_group_enter(group);
[[TZImageManager manager] getOriginalPhotoDataWithAsset:obj completion:^(NSData *data, NSDictionary *info, BOOL isDegraded) {
[dataArray addObject:data];
dispatch_group_leave(group);
}];
}];
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
finish(dataArray);
});
}
3.拿著數(shù)據(jù)進(jìn)行上傳就行了乓诽,最好名稱后綴再做個(gè)判斷是GIF的上傳的時(shí)候后綴改為.gif,這樣的話宏娄,如果項(xiàng)目有需要判斷GIF的情況可用地址后綴判斷
gif保存到本地相冊(cè)
1.獲取網(wǎng)絡(luò)請(qǐng)求后的NSData
現(xiàn)在網(wǎng)絡(luò)請(qǐng)請(qǐng)求加載圖片一般用SDWebImage和YYWebImage
這邊舉例YYWebImage
1.使用YYImageCache,傳入網(wǎng)絡(luò)圖片地址獲取圖片緩存
- (nullable NSData *)getImageDataForKey:(NSString *)key;
- (void)getImageDataForKey:(NSString *)key
withBlock:(void(^)(NSData * _Nullable imageData))block;
2.使用YYAnimatedImageView播放GIF的話可以直接抓出NSData數(shù)據(jù)
YYImage *image = (YYImage *)imageView.image;
//image.animatedImageData
//@property (nullable, nonatomic, readonly) NSData *animatedImageData;
2.將NSData保存到本地相冊(cè)
- (void)savePhotoWithData:(NSData *)data completion:(void (^)(NSError *error))completion {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
options.shouldMoveFile = YES;
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
request.creationDate = [NSDate date];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_sync(dispatch_get_main_queue(), ^{
if (success && completion) {
completion(nil);
} else if (error) {
NSLog(@"保存照片出錯(cuò):%@",error.localizedDescription);
if (completion) {
completion(error);
}
}
});
}];
}
gif的顯示
網(wǎng)上很多第三方框架问裕,這邊推薦一個(gè)YYAnimatedImageView