博客> iOS ALssert获取相册视频有效路径转换
iOS ALssert获取相册视频有效路径转换
2017-11-21 01:47 评论:0 阅读:414 674313234a
ios

直接取出ALAsset的url NSURL *url = [asset defaultRepresentation].url;
然后把这个url传入这个方法,就能获取到有效路径,然后根据自己需要上传服务器就行了。

  • (NSString)videoAssetURLToTempFile:(NSURL)url { NSString surl = [url absoluteString]; NSString ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4]; NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate]; NSString filename = [NSString stringWithFormat: @"%f.%@",ti,ext]; NSString tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename]; ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset myasset) { ALAssetRepresentation rep = [myasset defaultRepresentation]; NSUInteger size = [rep size]; const int bufferSize = 8192; NSLog(@"Writing to %@",tmpfile); FILE f = fopen([tmpfile cStringUsingEncoding:1], "wb+"); if (f == NULL) { NSLog(@"Can not create tmp file."); return; } Byte buffer = (Byte)malloc(bufferSize); int read = 0, offset = 0, written = 0; NSError err; if (size != 0) { do { read = (int)[rep getBytes:buffer fromOffset:offset length:bufferSize error:&err]; written = (int)fwrite(buffer, sizeof(char), read, f); offset += read; } while (read != 0);

    }
    fclose(f);

    };

    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError myerror) { NSLog(@"Can not get asset - %@",[myerror localizedDescription]); }; if(url) { ALAssetsLibrary assetslibrary = [[ALAssetsLibrary alloc] init]; [assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock]; } return [NSString stringWithFormat:@"file//:%@",tmpfile]; }

收藏
0
sina weixin mail 回到顶部