mirror of
https://github.com/DmcSDK/cordova-plugin-mediaPicker
synced 2026-05-03 00:00:04 +08:00
support ios
This commit is contained in:
@@ -229,7 +229,7 @@ public class MediaPicker extends CordovaPlugin {
|
||||
byte[] imageBytes = baos.toByteArray();
|
||||
encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
|
||||
baos.close();
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return encodedImage;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
|
||||
@interface CollectionViewCell : UICollectionViewCell
|
||||
@property(nonatomic ,strong)UIImageView *imgView;
|
||||
@property(nonatomic ,strong)UIImageView *whiteView;
|
||||
@property(nonatomic ,strong)UIImageView *checkView;
|
||||
@property(nonatomic ,strong)UILabel *labelL;
|
||||
@property(nonatomic ,strong)UILabel *labelR;
|
||||
@end
|
||||
@@ -0,0 +1,45 @@
|
||||
|
||||
|
||||
#import "CollectionViewCell.h"
|
||||
|
||||
@implementation CollectionViewCell
|
||||
- (id)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
CGFloat width=CGRectGetWidth(self.frame);
|
||||
|
||||
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
|
||||
self.imgView.contentMode=UIViewContentModeScaleAspectFill;
|
||||
self.imgView.clipsToBounds=YES;
|
||||
|
||||
CGFloat checkWidth=width/5;
|
||||
self.checkView=[[UIImageView alloc]initWithFrame:CGRectMake(width-checkWidth-5, 5, checkWidth, checkWidth)];
|
||||
self.whiteView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
|
||||
|
||||
|
||||
CGFloat labelH=width/6;
|
||||
self.labelL = [[UILabel alloc]initWithFrame:CGRectMake(0, width-labelH, width/2, labelH)];
|
||||
self.labelL.textColor=[UIColor whiteColor];
|
||||
self.labelL.font=[UIFont systemFontOfSize:13.0];
|
||||
self.labelL.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.3f];
|
||||
|
||||
|
||||
self.labelR = [[UILabel alloc]initWithFrame:CGRectMake(width/2, width-labelH, width/2, labelH)];
|
||||
self.labelR.textColor=[UIColor whiteColor];
|
||||
self.labelR.font=[UIFont systemFontOfSize:13.0];
|
||||
self.labelR.textAlignment=NSTextAlignmentRight;
|
||||
self.labelR.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.3f];
|
||||
|
||||
|
||||
|
||||
[self addSubview:self.imgView];
|
||||
[self addSubview:self.whiteView];
|
||||
[self addSubview:self.checkView];
|
||||
[self addSubview:self.labelL];
|
||||
[self addSubview:self.labelR];
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <Photos/Photos.h>
|
||||
@protocol DmcPickerDelegate<NSObject>
|
||||
-(void) resultPicker:(NSMutableArray*) selectArray;
|
||||
@end
|
||||
|
||||
@interface DmcPickerViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate>{
|
||||
NSMutableArray *_cellArray; //collectionView数据
|
||||
PHFetchResult * fetchResult;
|
||||
NSMutableArray *selectArray;
|
||||
}
|
||||
@property (nonatomic, strong) UICollectionView *collectionView;
|
||||
@property (nonatomic,weak)id<DmcPickerDelegate> _delegate;
|
||||
/// Default is 9 / 默认最大可选9张图片
|
||||
@property (nonatomic, assign) NSInteger maxSelectCount;
|
||||
//'selectMode':101,//101=PICKER_IMAGE_VIDEO , 100=PICKER_IMAGE , 102=PICKER_VIDEO
|
||||
@property (nonatomic, assign) NSInteger selectMode;
|
||||
@end
|
||||
@@ -0,0 +1,273 @@
|
||||
|
||||
|
||||
#import "DmcPickerViewController.h"
|
||||
#import "CollectionViewCell.h"
|
||||
#import <Photos/Photos.h>
|
||||
#define fDeviceWidth ([UIScreen mainScreen].bounds.size.width) //设备高度的宏
|
||||
#define fDeviceHeight ([UIScreen mainScreen].bounds.size.height)
|
||||
@interface DmcPickerViewController ()
|
||||
@property (strong, nonatomic) PHImageManager *manager;
|
||||
@end
|
||||
|
||||
@implementation DmcPickerViewController
|
||||
|
||||
- (void)viewDidLoad {
|
||||
//init config
|
||||
self.maxSelectCount=self.maxSelectCount>0?self.maxSelectCount:15;
|
||||
self.selectMode=self.selectMode>0?self.selectMode:101;
|
||||
//config end
|
||||
|
||||
[super viewDidLoad];
|
||||
[self initView];
|
||||
[self requestPermission];
|
||||
|
||||
|
||||
}
|
||||
|
||||
-(void)initView{
|
||||
|
||||
[[self view]setBackgroundColor:[UIColor greenColor]];
|
||||
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done",nil) style:UIBarButtonItemStylePlain target:self action:@selector(done)];
|
||||
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
|
||||
self.navigationItem.rightBarButtonItem = rightButtonItem;
|
||||
[self setDoneBtnStatus];
|
||||
self.navigationItem.leftBarButtonItem=leftButtonItem;
|
||||
self.navigationItem.title=NSLocalizedString(@"all",nil);
|
||||
|
||||
[self.view addSubview:self.collectionView];
|
||||
}
|
||||
|
||||
-(void) done{
|
||||
[self._delegate resultPicker:selectArray];
|
||||
[self dismissViewControllerAnimated:YES completion:nil];
|
||||
}
|
||||
|
||||
-(void) cancel{
|
||||
[self dismissViewControllerAnimated:YES completion:nil];
|
||||
}
|
||||
|
||||
-(void)requestPermission{
|
||||
//监测权限
|
||||
if ([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatusAuthorized) {
|
||||
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
|
||||
if ([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatusAuthorized) {
|
||||
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Unable to access album",nil) message:NSLocalizedString(@"Please allow to access your album",nil) preferredStyle:UIAlertControllerStyleAlert];
|
||||
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel",nil) style:UIAlertActionStyleDefault handler:nil]];
|
||||
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Setting",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
||||
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
|
||||
}]];
|
||||
[self presentViewController:alert animated:YES completion:nil];
|
||||
}else {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self getAlassetData];
|
||||
});
|
||||
}
|
||||
}];
|
||||
}else {
|
||||
[self getAlassetData];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) getAlassetData{
|
||||
|
||||
selectArray=[[NSMutableArray alloc] init];
|
||||
|
||||
// 获取所有资源的集合,并按资源的创建时间排序
|
||||
PHFetchOptions *options = [[PHFetchOptions alloc] init];
|
||||
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
|
||||
if(self.selectMode==100){
|
||||
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
|
||||
}else if(self.selectMode==102){
|
||||
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo];
|
||||
}
|
||||
fetchResult = [PHAsset fetchAssetsWithOptions:options];
|
||||
|
||||
_manager = [PHImageManager defaultManager];
|
||||
|
||||
[_collectionView reloadData];
|
||||
}
|
||||
|
||||
|
||||
|
||||
#pragma mark - 创建collectionView并设置代理
|
||||
- (UICollectionView *)collectionView
|
||||
{
|
||||
if (_collectionView == nil) {
|
||||
|
||||
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
|
||||
|
||||
|
||||
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight) collectionViewLayout:flowLayout];
|
||||
|
||||
int count=3;
|
||||
if([[UIDevice currentDevice].model isEqualToString:@"iPad"]){
|
||||
count=8;
|
||||
}
|
||||
//定义每个UICollectionView 的大小
|
||||
flowLayout.itemSize = CGSizeMake((fDeviceWidth-2)/count, (fDeviceWidth-2)/count);
|
||||
//定义每个UICollectionView 横向的间距
|
||||
flowLayout.minimumLineSpacing = 1;
|
||||
//定义每个UICollectionView 纵向的间距
|
||||
flowLayout.minimumInteritemSpacing = 1;
|
||||
//定义每个UICollectionView 的边距距
|
||||
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 5, 0);//上左下右
|
||||
|
||||
//注册cell和ReusableView(相当于头部)
|
||||
[_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
|
||||
//[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
|
||||
|
||||
//设置代理
|
||||
_collectionView.delegate = self;
|
||||
_collectionView.dataSource = self;
|
||||
|
||||
//背景颜色
|
||||
_collectionView.backgroundColor = [UIColor whiteColor];
|
||||
//自适应大小
|
||||
_collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
||||
|
||||
}
|
||||
return _collectionView;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma mark - UICollectionView delegate dataSource
|
||||
#pragma mark 定义展示的UICollectionViewCell的个数
|
||||
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
||||
{
|
||||
return fetchResult.count;
|
||||
}
|
||||
|
||||
#pragma mark 定义展示的Section的个数
|
||||
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
#pragma mark 每个UICollectionView展示的内容
|
||||
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
static NSString *identify = @"cell";
|
||||
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
|
||||
[cell sizeToFit];
|
||||
PHAsset *asset=fetchResult[indexPath.item];
|
||||
[_manager requestImageForAsset:asset
|
||||
targetSize:CGSizeMake(200 , 200)
|
||||
contentMode:PHImageContentModeAspectFill
|
||||
options:nil
|
||||
resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
|
||||
cell.imgView.image = result;
|
||||
}];
|
||||
NSInteger i=[self isSelect:asset];
|
||||
if(asset.mediaType==PHAssetMediaTypeVideo){
|
||||
cell.labelL.hidden=NO;
|
||||
cell.labelR.hidden=NO;
|
||||
|
||||
NSString *dtime=[NSString stringWithFormat:@"%.0f",asset.duration];
|
||||
cell.labelL.text=[@"\t"stringByAppendingString:NSLocalizedString(@"Video",nil)];
|
||||
cell.labelR.text=[[self getNewTimeFromDurationSecond:dtime.integerValue]stringByAppendingString:@"\t"];
|
||||
}else{
|
||||
cell.labelL.hidden=YES;
|
||||
cell.labelR.hidden=YES;
|
||||
}
|
||||
|
||||
if(i<0){
|
||||
[self hidenSelectView:cell];
|
||||
}else{
|
||||
[self showSelectView:cell];
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
||||
|
||||
//UICollectionView被选中时调用的方法
|
||||
-( void )collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath{
|
||||
NSLog(@"%ld",(long)indexPath.row);
|
||||
PHAsset * asset=fetchResult[indexPath.row];
|
||||
NSInteger i=[self isSelect:asset];
|
||||
CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
|
||||
|
||||
if([selectArray count]>=self.maxSelectCount&&i<0){
|
||||
[self alertMax];
|
||||
}else{
|
||||
if([selectArray count]>self.maxSelectCount){
|
||||
[self alertMax];
|
||||
}else{
|
||||
i<0?[selectArray addObject:asset]:[selectArray removeObject:asset];
|
||||
i<0?[self showSelectView:cell]:[self hidenSelectView:cell];
|
||||
}
|
||||
}
|
||||
|
||||
[self setDoneBtnStatus];
|
||||
}
|
||||
|
||||
-(void)setDoneBtnStatus{
|
||||
if([selectArray count]>0){
|
||||
self.navigationItem.rightBarButtonItem.enabled= YES;
|
||||
}else{
|
||||
self.navigationItem.rightBarButtonItem.enabled= NO;
|
||||
}
|
||||
}
|
||||
|
||||
-(void)showSelectView:( CollectionViewCell *)cell{
|
||||
cell.checkView.hidden=NO;
|
||||
cell.whiteView.hidden=NO;
|
||||
cell.whiteView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
|
||||
cell.checkView.image=[UIImage imageNamed:@"dmcPicker.bundle/select80.png"];
|
||||
}
|
||||
|
||||
-(void)hidenSelectView:( CollectionViewCell *)cell{
|
||||
cell.checkView.hidden=YES;
|
||||
cell.whiteView.hidden=YES;
|
||||
}
|
||||
|
||||
|
||||
|
||||
-(NSInteger)isSelect:(PHAsset *)asset
|
||||
{
|
||||
int is=-1;
|
||||
if([selectArray count]<=0){
|
||||
return is;
|
||||
}
|
||||
for(NSInteger i=0;i<[selectArray count];i++){
|
||||
PHAsset *now=selectArray[i];
|
||||
if ([asset.localIdentifier isEqualToString:now.localIdentifier]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning {
|
||||
[super didReceiveMemoryWarning];
|
||||
}
|
||||
|
||||
-(void)alertMax{
|
||||
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
|
||||
message:NSLocalizedString(@"maxSelectAlert",nil)
|
||||
preferredStyle:UIAlertControllerStyleAlert];
|
||||
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok",nil) style:UIAlertActionStyleDefault handler:nil];
|
||||
[alertController addAction:okAction];
|
||||
|
||||
[self presentViewController:alertController animated:YES completion:nil];
|
||||
}
|
||||
|
||||
- (NSString *)getNewTimeFromDurationSecond:(NSInteger)duration {
|
||||
NSString *newTime;
|
||||
if (duration < 10) {
|
||||
newTime = [NSString stringWithFormat:@"0:0%ld",(long)duration];
|
||||
} else if (duration < 60) {
|
||||
newTime = [NSString stringWithFormat:@"0:%ld",(long)duration];
|
||||
} else {
|
||||
NSInteger min = duration / 60;
|
||||
NSInteger sec = duration - (min * 60);
|
||||
if (sec < 10) {
|
||||
newTime = [NSString stringWithFormat:@"%ld:0%ld",(long)min,(long)sec];
|
||||
} else {
|
||||
newTime = [NSString stringWithFormat:@"%ld:%ld",(long)min,(long)sec];
|
||||
}
|
||||
}
|
||||
return newTime;
|
||||
}
|
||||
@end
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.6 KiB |
@@ -0,0 +1,9 @@
|
||||
"Cancel"="Cancel";
|
||||
"Done"="Done";
|
||||
"all"="all";
|
||||
"Video"="Video";
|
||||
"maxSelectAlert"="Has reached the maximum number of choices";
|
||||
"ok"="OK";
|
||||
"Unable to access album" = "Unable to access album";
|
||||
"Please allow to access your album" = "Please allow to access your album in \"Settings -> Privacy -> Album\"";
|
||||
"Setting" = "Setting";
|
||||
@@ -0,0 +1,9 @@
|
||||
"Cancel"="取消";
|
||||
"Done"="完成";
|
||||
"All"="所有";
|
||||
"Video"="视频";
|
||||
"maxSelectAlert"="已达到选择数量上限";
|
||||
"ok"="好";
|
||||
"Unable to access album" = "无法访问相册";
|
||||
"Please allow to access your album" = "请在设置-隐私-相册中允许访问相册";
|
||||
"Setting" = "设置";
|
||||
@@ -0,0 +1,243 @@
|
||||
/********* MediaPicker.m Cordova Plugin Implementation *******/
|
||||
|
||||
#import <Cordova/CDV.h>
|
||||
#import "DmcPickerViewController.h"
|
||||
@interface MediaPicker : CDVPlugin <DmcPickerDelegate>{
|
||||
// Member variables go here.
|
||||
NSString* callbackId;
|
||||
}
|
||||
|
||||
- (void)getMedias:(CDVInvokedUrlCommand*)command;
|
||||
- (void)takePhoto:(CDVInvokedUrlCommand*)command;
|
||||
- (void)extractThumbnail:(CDVInvokedUrlCommand*)command;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MediaPicker
|
||||
|
||||
- (void)getMedias:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
callbackId=command.callbackId;
|
||||
NSDictionary *options = [command.arguments objectAtIndex: 0];
|
||||
DmcPickerViewController * dmc=[[DmcPickerViewController alloc] init];
|
||||
@try{
|
||||
dmc.selectMode=[[options objectForKey:@"selectMode"]integerValue];
|
||||
}@catch (NSException *exception) {
|
||||
NSLog(@"Exception: %@", exception);
|
||||
}
|
||||
@try{
|
||||
dmc.maxSelectCount=[[options objectForKey:@"maxSelectCount"]integerValue];
|
||||
}@catch (NSException *exception) {
|
||||
NSLog(@"Exception: %@", exception);
|
||||
}
|
||||
dmc._delegate=self;
|
||||
[self.viewController presentViewController:[[UINavigationController alloc]initWithRootViewController:dmc] animated:YES completion:nil];
|
||||
}
|
||||
|
||||
-(void) resultPicker:(NSMutableArray*) selectArray
|
||||
{
|
||||
|
||||
NSString * tmpDir = NSTemporaryDirectory();
|
||||
NSString *dmcPickerPath = [tmpDir stringByAppendingPathComponent:@"dmcPicker"];
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
if(![fileManager fileExistsAtPath:dmcPickerPath ]){
|
||||
[fileManager createDirectoryAtPath:dmcPickerPath withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
}
|
||||
|
||||
NSMutableArray * aListArray=[[NSMutableArray alloc] init];
|
||||
dispatch_async(dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
int index=0;
|
||||
for(PHAsset *asset in selectArray){
|
||||
@autoreleasepool {
|
||||
if(asset.mediaType==PHAssetMediaTypeImage){
|
||||
[self imageToSandbox:asset dmcPickerPath:dmcPickerPath aListArray:aListArray selectArray:selectArray index:index];
|
||||
}else{
|
||||
[self videoToSandboxCompress:asset dmcPickerPath:dmcPickerPath aListArray:aListArray selectArray:selectArray index:index];
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
-(void)imageToSandbox:(PHAsset *)asset dmcPickerPath:(NSString*)dmcPickerPath aListArray:(NSMutableArray*)aListArray selectArray:(NSMutableArray*)selectArray index:(int)index{
|
||||
|
||||
|
||||
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
|
||||
|
||||
NSString *fullpath=[NSString stringWithFormat:@"%@/%@.%@", dmcPickerPath,[[NSProcessInfo processInfo] globallyUniqueString], @"jpg"];
|
||||
NSNumber *size=[NSNumber numberWithInt:imageData.length];
|
||||
NSError *error = nil;
|
||||
if (![imageData writeToFile:fullpath options:NSAtomicWrite error:&error]) {
|
||||
NSLog(@"%@", [error localizedDescription]);
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]] callbackId:callbackId];
|
||||
} else {
|
||||
|
||||
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:fullpath,@"path",@"image",@"mediaType",size,@"size",[NSNumber numberWithInt:index],@"index", nil];
|
||||
[aListArray addObject:dict];
|
||||
if([aListArray count]==[selectArray count]){
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:aListArray] callbackId:callbackId];
|
||||
}
|
||||
}
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
|
||||
-(void)videoToSandbox:(PHAsset *)asset dmcPickerPath:(NSString*)dmcPickerPath aListArray:(NSMutableArray*)aListArray selectArray:(NSMutableArray*)selectArray index:(int)index{
|
||||
|
||||
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *avsset, AVAudioMix *audioMix, NSDictionary *info) {
|
||||
if ([avsset isKindOfClass:[AVURLAsset class]]) {
|
||||
NSString *filename = [asset valueForKey:@"filename"];
|
||||
AVURLAsset* urlAsset = (AVURLAsset*)avsset;
|
||||
|
||||
NSString *fullpath=[NSString stringWithFormat:@"%@/%@", dmcPickerPath,filename];
|
||||
NSLog(@"%@", urlAsset.URL);
|
||||
NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL options:NSDataReadingUncached error:nil];
|
||||
NSUInteger size=data.length;
|
||||
NSError *error = nil;
|
||||
if (![data writeToFile:fullpath options:NSAtomicWrite error:&error]) {
|
||||
NSLog(@"%@", [error localizedDescription]);
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]] callbackId:callbackId];
|
||||
} else {
|
||||
|
||||
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:fullpath,@"path",size,@"size",@"video",@"mediaType" ,[NSNumber numberWithInt:index],@"index", nil];
|
||||
[aListArray addObject:dict];
|
||||
if([aListArray count]==[selectArray count]){
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:aListArray] callbackId:callbackId];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}];
|
||||
|
||||
}
|
||||
|
||||
-(void)videoToSandboxCompress:(PHAsset *)asset dmcPickerPath:(NSString*)dmcPickerPath aListArray:(NSMutableArray*)aListArray selectArray:(NSMutableArray*)selectArray index:(int)index{
|
||||
|
||||
[[PHImageManager defaultManager] requestExportSessionForVideo:asset options:nil exportPreset:AVAssetExportPresetPassthrough resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {
|
||||
|
||||
|
||||
NSString *fullpath=[NSString stringWithFormat:@"%@/%@.%@", dmcPickerPath,[[NSProcessInfo processInfo] globallyUniqueString], @"mp4"];
|
||||
NSURL *outputURL = [NSURL fileURLWithPath:fullpath];
|
||||
|
||||
NSLog(@"this is the final path %@",outputURL);
|
||||
|
||||
exportSession.outputFileType=AVFileTypeMPEG4;
|
||||
|
||||
exportSession.outputURL=outputURL;
|
||||
|
||||
[exportSession exportAsynchronouslyWithCompletionHandler:^{
|
||||
|
||||
if (exportSession.status == AVAssetExportSessionStatusFailed) {
|
||||
|
||||
NSLog(@"failed");
|
||||
|
||||
} else if(exportSession.status == AVAssetExportSessionStatusCompleted){
|
||||
|
||||
NSLog(@"completed!");
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:fullpath,@"path",@"video",@"mediaType" ,[NSNumber numberWithInt:index],@"index", nil];
|
||||
[aListArray addObject:dict];
|
||||
if([aListArray count]==[selectArray count]){
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:aListArray] callbackId:callbackId];
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}];
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
-(NSString*)thumbnailVideo:(NSString*)path quality:(NSInteger)quality {
|
||||
UIImage *shotImage;
|
||||
//视频路径URL
|
||||
NSURL *fileURL = [NSURL fileURLWithPath:path];
|
||||
|
||||
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
|
||||
|
||||
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
|
||||
|
||||
gen.appliesPreferredTrackTransform = YES;
|
||||
|
||||
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
|
||||
|
||||
NSError *error = nil;
|
||||
|
||||
CMTime actualTime;
|
||||
|
||||
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
|
||||
|
||||
shotImage = [[UIImage alloc] initWithCGImage:image];
|
||||
|
||||
CGImageRelease(image);
|
||||
CGFloat q=quality/100.0f;
|
||||
NSString *thumbnail=[UIImageJPEGRepresentation(shotImage,q) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
- (void)takePhoto:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
-(NSString*)thumbnailImage:(NSString*)path quality:(NSInteger)quality{
|
||||
UIImage *result = [[UIImage alloc] initWithContentsOfFile:path];
|
||||
NSInteger qu = quality>0?quality:3;
|
||||
CGFloat q=qu/100.0f;
|
||||
NSString *thumbnail=[UIImageJPEGRepresentation(result,q) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
|
||||
|
||||
- (void)extractThumbnail:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
callbackId=command.callbackId;
|
||||
NSMutableDictionary *options = [command.arguments objectAtIndex: 0];
|
||||
NSString *thumbnail;
|
||||
if([@"image" isEqualToString: [options objectForKey:@"mediaType"]]){
|
||||
thumbnail=[self thumbnailImage:[options objectForKey:@"path"] quality:[[options objectForKey:@"thumbnailQuality"] integerValue]];
|
||||
}else{
|
||||
thumbnail=[self thumbnailVideo:[options objectForKey:@"path"] quality:[[options objectForKey:@"thumbnailQuality"] integerValue]];
|
||||
}
|
||||
|
||||
|
||||
[options setObject:thumbnail forKey:@"thumbnailBase64"];
|
||||
NSNumber* rotate = [NSNumber numberWithInt:0];
|
||||
[options setObject:rotate forKey:@"exifRotate"];
|
||||
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:options] callbackId:callbackId];
|
||||
}
|
||||
|
||||
-(int)getOrientation:(UIImage *)image{
|
||||
switch (image.imageOrientation) {
|
||||
case UIImageOrientationDown:
|
||||
return 180;
|
||||
case UIImageOrientationDownMirrored:
|
||||
return 180;
|
||||
case UIImageOrientationLeft:
|
||||
return 270;
|
||||
case UIImageOrientationLeftMirrored:
|
||||
return 270;
|
||||
case UIImageOrientationRight:
|
||||
return 90;
|
||||
case UIImageOrientationRightMirrored:
|
||||
return 90;
|
||||
case UIImageOrientationUp:
|
||||
return 0;
|
||||
case UIImageOrientationUpMirrored:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user