獲取本地視頻&&圖像

// 照片原圖路徑

#define KOriginalPhotoImagePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"OriginalPhotoImages"]

// 視頻URL路徑

#define KVideoUrlPath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@VideoURL]

// caches路徑

#define KCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]

//獲取手機(jī)video ?and ?image

- (void)getVideoAndImage

{

__weak typeof(self) weakSelf = self;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {

if (group != nil) {

[weakSelf.groupArrays addObject:group];

} else {

[weakSelf.groupArrays enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

[obj enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

if ([result thumbnail] != nil) {

// 照片

if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]){

NSDate *date= [result valueForProperty:ALAssetPropertyDate];

UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];

NSString *fileName = [[result defaultRepresentation] filename];

NSURL *url = [[result defaultRepresentation] url];

int64_t fileSize = [[result defaultRepresentation] size];

NSLog(@"date = %@",date);

NSLog(@"fileName = %@",fileName);

NSLog(@"url = %@",url);

NSLog(@"fileSize = %lld",fileSize);

// UI的更新記得放在主線程,要不然等子線程排隊過來都不知道什么年代了,會很慢的

dispatch_async(dispatch_get_main_queue(), ^{

self.litimgView.image = image;

});

}

// 視頻

else if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo] ){

NSDate *date= [result valueForProperty:ALAssetPropertyDate];

UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];

NSString *fileName = [[result defaultRepresentation] filename];

NSURL *url = [[result defaultRepresentation] url];

int64_t fileSize = [[result defaultRepresentation] size];

self.urlString = url;

NSLog(@"video----date = %@",date);

NSLog(@"video----fileName = %@",fileName);

NSLog(@"video----url = %@",url);

NSLog(@"video----fileSize = %lld",fileSize);

// 和圖片方法類似

dispatch_async(dispatch_get_main_queue(), ^{

self.litimgView.image = image;

});

}

}

}];

}];

}

};

ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){

NSString *errorMessage = nil;

switch ([error code]) {

case ALAssetsLibraryAccessUserDeniedError:

case ALAssetsLibraryAccessGloballyDeniedError:

errorMessage = @"用戶拒絕訪問相冊,請在<隱私>中開啟";

break;

default:

errorMessage = @"Reason unknown.";

break;

}

dispatch_async(dispatch_get_main_queue(), ^{

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"錯誤,無法訪問!" message:errorMessage delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

[alertView show];

});

};

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]? init];

[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

usingBlock:listGroupBlock failureBlock:failureBlock];

});

}


// 將原始圖片的URL轉(zhuǎn)化為NSData數(shù)據(jù),寫入沙盒

- (void)imageWithUrl:(NSURL *)url withFileName:(NSString *)fileName

{

// 進(jìn)這個方法的時候也應(yīng)該加判斷,如果已經(jīng)轉(zhuǎn)化了的就不要調(diào)用這個方法了

// 如何判斷已經(jīng)轉(zhuǎn)化了,通過是否存在文件路徑

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

// 創(chuàng)建存放原始圖的文件夾--->OriginalPhotoImages

NSFileManager * fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:KOriginalPhotoImagePath]) {

[fileManager createDirectoryAtPath:KOriginalPhotoImagePath withIntermediateDirectories:YES attributes:nil error:nil];

}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

if (url) {

// 主要方法

[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {

ALAssetRepresentation *rep = [asset defaultRepresentation];

Byte *buffer = (Byte*)malloc((unsigned long)rep.size);

NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:((unsigned long)rep.size) error:nil];

NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

NSString * imagePath = [KOriginalPhotoImagePath stringByAppendingPathComponent:fileName];

[data writeToFile:imagePath atomically:YES];

} failureBlock:nil];

}

});

}

// 將原始視頻的URL轉(zhuǎn)化為NSData數(shù)據(jù),寫入沙盒

- (void)videoWithUrl:(NSURL *)url withFileName:(NSString *)fileName

{

// 解析一下,為什么視頻不像圖片一樣一次性開辟本身大小的內(nèi)存寫入?

// 想想,如果1個視頻有1G多,難道直接開辟1G多的空間大小來寫?

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

if (url) {

[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {

ALAssetRepresentation *rep = [asset defaultRepresentation];

NSString * videoPath = [KCachesPath stringByAppendingPathComponent:fileName];

char const *cvideoPath = [videoPath UTF8String];

FILE *file = fopen(cvideoPath, "a+");

if (file) {

const int bufferSize = 1024 * 1024;

// 初始化一個1M的buffer

Byte *buffer = (Byte*)malloc(bufferSize);

NSUInteger read = 0, offset = 0, written = 0;

NSError* err = nil;

if (rep.size != 0)

{

do {

read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err];

written = fwrite(buffer, sizeof(char), read, file);

offset += read;

} while (read != 0 && !err);//沒到結(jié)尾,沒出錯,ok繼續(xù)

}

// 釋放緩沖區(qū),關(guān)閉文件

free(buffer);

buffer = NULL;

fclose(file);

file = NULL;

}

} failureBlock:nil];

}

});

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容