diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d8b1490..ee9f1578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# CHANGELOG + +## 0.1.1 fix ios video + +fix 'ios video full file is a jpg' problem + ## 0.1.0 support video support video in android. @@ -13,4 +19,4 @@ update for the issue #1 (NPE when request other permission on android) first version -api +api for photo \ No newline at end of file diff --git a/README.md b/README.md index 62214544..6b91def2 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ because support video, so rename api from ImageXXXX to AssetXXXX ```yaml dependencies: - photo_manager: ^0.1.0 + photo_manager: ^0.1.1 ``` ## import diff --git a/example/lib/photo_list.dart b/example/lib/photo_list.dart index 24ac2956..e4b4ce35 100644 --- a/example/lib/photo_list.dart +++ b/example/lib/photo_list.dart @@ -32,24 +32,27 @@ class _PhotoListState extends State { future: entity.thumbData, builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.data != null) { - return Stack( - children: [ - Image.memory( - snapshot.data, - fit: BoxFit.cover, - width: double.infinity, - height: double.infinity, - ), - IgnorePointer( - child: Container( - alignment: Alignment.center, - child: Text( - '${entity.type}', - style: TextStyle(color: Colors.white), + return InkWell( + onTap: () => showInfo(entity), + child: Stack( + children: [ + Image.memory( + snapshot.data, + fit: BoxFit.cover, + width: double.infinity, + height: double.infinity, + ), + IgnorePointer( + child: Container( + alignment: Alignment.center, + child: Text( + '${entity.type}', + style: TextStyle(color: Colors.white), + ), ), ), - ), - ], + ], + ), ); } return Center( @@ -58,4 +61,12 @@ class _PhotoListState extends State { }, ); } + + showInfo(AssetEntity entity) async { + if (entity.type == AssetType.video) { + var file = await entity.file; + var length = file.lengthSync(); + print("${entity.id} length = $length"); + } + } } diff --git a/ios/Classes/ImageScanner.m b/ios/Classes/ImageScanner.m index 88340e81..2e172b6c 100644 --- a/ios/Classes/ImageScanner.m +++ b/ios/Classes/ImageScanner.m @@ -225,17 +225,75 @@ - (void)getFullFileWithCall:(FlutterMethodCall *)call result:(FlutterResult)flut NSString *imgId = call.arguments; - PHAsset *asset = _idAssetDict[imgId]; - + PHAsset *asset = self->_idAssetDict[imgId]; __weak ImageScanner *wSelf = self; - - [manager requestImageDataForAsset:asset options:[PHImageRequestOptions new] resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { - NSString *path = [wSelf writeFullFileWithAssetId:asset imageData:imageData]; - flutterResult(path); - }]; + if([asset isImage]) { + [manager requestImageDataForAsset:asset options:[PHImageRequestOptions new] resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { + NSString *path = [wSelf writeFullFileWithAssetId:asset imageData:imageData]; + flutterResult(path); + }]; + } else if([asset isVideo]) { + PHVideoRequestOptions *options = [PHVideoRequestOptions new]; + options.version = PHVideoRequestOptionsVersionCurrent; +// NSString *path = + [self writeFullVideoFileWithAsset:asset result:flutterResult]; +// flutterResult(path); + } else { + flutterResult(nil); + } }); } +- (void) writeFullVideoFileWithAsset:(PHAsset*) asset result:(FlutterResult)result{ + NSString *homePath = NSTemporaryDirectory(); + NSFileManager *manager = NSFileManager.defaultManager; + + NSMutableString *path = [NSMutableString stringWithString:homePath]; + + NSString *filename = [asset valueForKey:@"filename"]; + + NSString *dirPath = [NSString stringWithFormat:@"%@/%@",homePath,@".video"]; + [manager createDirectoryAtPath:dirPath attributes:@{}]; + + [path appendFormat:@"%@/%@",@".video",filename]; + PHVideoRequestOptions *options = [PHVideoRequestOptions new]; + if([manager fileExistsAtPath:path]){ + NSLog(@"read cache from %@",path); + result(path); + return; + } + [[PHImageManager defaultManager]requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) { + NSURL *fileRUL = [asset valueForKey:@"URL"]; + NSData *beforeVideoData = [NSData dataWithContentsOfURL:fileRUL];//未压缩的视频流 + BOOL createResult = [manager createFileAtPath:path contents:beforeVideoData attributes:@{}]; + NSLog(@"write file to %@ , size = %lu , createResult = %@",path,(unsigned long)beforeVideoData.length,createResult?@"true":@"false"); + result(path); + }]; +// +// +// if(@available(iOS 9.0, *)){ +// NSArray *a = [PHAssetResource assetResourcesForAsset:asset]; +// PHAssetResource *resource = a.firstObject; +// +// if (resource.originalFilename) { +// filename = resource.originalFilename; +// } +// [path appendFormat:@"%@/%@",@".video",filename]; +// if([manager fileExistsAtPath:path]){ +// result(path); +// return; +// } +// PHAssetResourceRequestOptions * options = [[PHAssetResourceRequestOptions alloc] init]; +// +// [[PHAssetResourceManager defaultManager]writeDataForAssetResource:resource toFile:[NSURL fileURLWithPath:filename] options:options completionHandler:^(NSError * _Nullable error) { +// result(path); +// }]; +// +// } else { +// +// } +} + - (NSString *)writeFullFileWithAssetId:(PHAsset *)asset imageData:(NSData *)imageData { NSString *homePath = NSTemporaryDirectory(); NSFileManager *manager = NSFileManager.defaultManager; diff --git a/pubspec.yaml b/pubspec.yaml index 49f7967c..3d4551cb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: photo_manager description: You can scan photos and albums. Only api, not have ui. you can use the api to create your image picker. or use photo -version: 0.1.0 +version: 0.1.1 author: caijinglong homepage: https://github.com/CaiJingLong/flutter_photo_manager