查看具有进度视图的多个视频上传示例。 在这里,我已经定义了步骤。
任何人都知道如何做到这一点,我会对我有所帮助。
可能我们可以使用NSUrlsession UPload任务但无法实现。
答案 0 :(得分:1)
您可以使用以下方法生成所选视频的拇指图像。
- (UIImage *)generateThumbImage : (NSString *)filepath {
NSURL *url = [NSURL fileURLWithPath:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
time.value = 2;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
return thumbnail;
}
为此,您可以使用AFNetworking 3.0。并创建一个NSObject
文件类来管理您的所有文件。创建具有imageView和progressView的collectionView。该collectionView类型是文件类型。
@interface File : NSObject
@property (nonatomic, strong) NSString *fullFilePath;
@property (nonatomic) float overAllProgress;
- (void)sendFile;
@end
@implementation File
- (void)sendFile {
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:self.fullFilePath] name:@"photo_path" fileName:self.relativePath mimeType:@"video/quicktime" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
self.overAllProgress = uploadProgress.fractionCompleted;
[[NSNotificationCenter defaultCenter] postNotificationName:@"imageprogress" object:self]
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
@end
现在您需要处理文件进度通知。如下所示。
-(void) viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileProgress:) name:@"imageprogress" object:nil];
}
- (void)viewDidUnload{
[super viewDidUnload];
// Release any retained subviews of the main view.
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"imageprogress" object:nil];
}
- (void)fileProgress:(NSNotification *)notif{
File * info = [notif object];
if([_arrFiles containsObject:info]){
NSInteger row = [_arrFiles indexOfObject:info];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
[cell.progressView setProgress:info.overAllProgress animated:YES]
}
}