Skip to content

Commit

Permalink
Merge pull request #7 from CaiJingLong/0.1.1-pre
Browse files Browse the repository at this point in the history
fix #7 bug and update version to 0.1.1-pre
  • Loading branch information
CaiJingLong authored Oct 12, 2018
2 parents 6075908 + 68725fe commit d9e1399
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 26 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -13,4 +19,4 @@ update for the issue #1 (NPE when request other permission on android)

first version

api
api for photo
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 27 additions & 16 deletions example/lib/photo_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,27 @@ class _PhotoListState extends State<PhotoList> {
future: entity.thumbData,
builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
if (snapshot.connectionState == ConnectionState.done && snapshot.data != null) {
return Stack(
children: <Widget>[
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: <Widget>[
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(
Expand All @@ -58,4 +61,12 @@ class _PhotoListState extends State<PhotoList> {
},
);
}

showInfo(AssetEntity entity) async {
if (entity.type == AssetType.video) {
var file = await entity.file;
var length = file.lengthSync();
print("${entity.id} length = $length");
}
}
}
72 changes: 65 additions & 7 deletions ios/Classes/ImageScanner.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<PHAssetResource *> *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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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<[email protected]>
homepage: https://github.com/CaiJingLong/flutter_photo_manager

Expand Down

0 comments on commit d9e1399

Please sign in to comment.