mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-17 23:18:27 +02:00
Make a copy of a video file before uploading
This commit is contained in:
@@ -797,46 +797,25 @@ NSString* MXKFileSizes_description(MXKFileSizes sizes)
|
||||
|
||||
if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:sendVideoAsset:withThumbnail:)])
|
||||
{
|
||||
if (![selectedVideo isKindOfClass:[AVURLAsset class]]) {
|
||||
MXLogDebug(@"sendSelectedVideoAsset failed because asset is not an AVURLAsset");
|
||||
return;
|
||||
}
|
||||
|
||||
// Retrieve the video frame at 1 sec to define the video thumbnail
|
||||
AVAssetImageGenerator *assetImageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:selectedVideo];
|
||||
assetImageGenerator.appliesPreferredTrackTransform = YES;
|
||||
CMTime time = CMTimeMake(1, 1);
|
||||
// CGImageRef imageRef = [assetImageGenerator copyCGImageAtTime:time actualTime:NULL error:nil];
|
||||
|
||||
if ([selectedVideo isKindOfClass:[AVURLAsset class]]) {
|
||||
NSLog(@"AVURLAsset");
|
||||
CGImageRef imageRef = [assetImageGenerator copyCGImageAtTime:time actualTime:NULL error:nil];
|
||||
if(imageRef) {
|
||||
// Finalize video attachment
|
||||
UIImage* videoThumbnail = [[UIImage alloc] initWithCGImage:imageRef];
|
||||
CFRelease(imageRef);
|
||||
|
||||
[self.delegate roomInputToolbarView:self sendVideoAsset:selectedVideo withThumbnail:videoThumbnail];
|
||||
} else {
|
||||
NSLog(@"Kein AVURLAsset");
|
||||
MXLogDebug(@"sendSelectedVideoAsset failed because imageRef is nil");
|
||||
}
|
||||
// dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
|
||||
[assetImageGenerator generateCGImagesAsynchronouslyForTimes:@[[NSValue valueWithCMTime:time]] completionHandler:^(CMTime requestedTime, CGImageRef cgImage, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
|
||||
// dispatch_async(dispatch_get_main_queue(), ^{
|
||||
NSLog(@"Error: %@", error);
|
||||
NSLog(@"Error: %@", error.localizedDescription);
|
||||
|
||||
NSError *dummy = error;
|
||||
|
||||
if (result == AVAssetImageGeneratorSucceeded) {
|
||||
UIImage *thumbnailImage = [UIImage imageWithCGImage:cgImage];
|
||||
NSLog(@"success: %@", thumbnailImage);
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
[self.delegate roomInputToolbarView:self sendVideoAsset:selectedVideo withThumbnail:thumbnailImage];
|
||||
});
|
||||
} else if (result == AVAssetImageGeneratorFailed) {
|
||||
NSLog(@"Error: %@", error.localizedDescription);
|
||||
|
||||
NSLog(@"Fehler beim Erstellen des Thumbnails: %@", error);
|
||||
} else if (result == AVAssetImageGeneratorCancelled) {
|
||||
NSLog(@"Thumbnail-Erstellung abgebrochen");
|
||||
}
|
||||
// });
|
||||
}];
|
||||
// });
|
||||
// Finalize video attachment
|
||||
// UIImage* videoThumbnail = [[UIImage alloc] initWithCGImage:imageRef];
|
||||
// CFRelease(imageRef);
|
||||
|
||||
// [self.delegate roomInputToolbarView:self sendVideoAsset:selectedVideo withThumbnail:videoThumbnail];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -2693,6 +2693,22 @@ static CGSize kThreadListBarButtonItemImageSize;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)sendVideoAssetWithoutCompression:(AVAsset *)videoAsset isPhotoLibraryAsset:(BOOL)isPhotoLibraryAsset
|
||||
{
|
||||
// Create before sending the message in case of a discussion (direct chat)
|
||||
[self createDiscussionIfNeeded:^(BOOL readyToSend) {
|
||||
if (readyToSend && [self inputToolbarConformsToToolbarViewProtocol])
|
||||
{
|
||||
[self.inputToolbarView sendSelectedVideoAsset:videoAsset isPhotoLibraryAsset:isPhotoLibraryAsset];
|
||||
} else {
|
||||
if([videoAsset isKindOfClass:[AVURLAsset class]]) {
|
||||
NSURL *url = [(AVURLAsset *)videoAsset URL];
|
||||
[self deleteFileAtURL:url];
|
||||
}
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)showRoomWithId:(NSString*)roomId
|
||||
{
|
||||
if (self.delegate)
|
||||
@@ -8060,58 +8076,100 @@ static CGSize kThreadListBarButtonItemImageSize;
|
||||
|
||||
if ([itemProvider canLoadObjectOfClass:[UIImage class]]) {
|
||||
[itemProvider loadObjectOfClass:[UIImage class] completionHandler:^(id _Nullable object, NSError * _Nullable error) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if ([object isKindOfClass:[UIImage class]]) {
|
||||
[self createDiscussionIfNeeded:^(BOOL readyToSend) {
|
||||
if (readyToSend && [self inputToolbarConformsToToolbarViewProtocol])
|
||||
{
|
||||
UIImage *image = object;
|
||||
NSData *imageData = [image dataForPNGRepresentation];
|
||||
if(imageData) {
|
||||
[self.inputToolbarView sendSelectedImage:imageData
|
||||
withMimeType:@"image/png"
|
||||
andCompressionMode:MediaCompressionHelper.defaultCompressionMode
|
||||
isPhotoLibraryAsset:YES];
|
||||
}
|
||||
}
|
||||
}];
|
||||
}
|
||||
});
|
||||
if (error) {
|
||||
MXLogDebug(@"Uploading photo from photo library failed: %@", error);
|
||||
} else {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if ([object isKindOfClass:[UIImage class]]) {
|
||||
UIImage *image = object;
|
||||
[self uploadImage:image];
|
||||
}
|
||||
});
|
||||
}
|
||||
}];
|
||||
} else if ([itemProvider hasItemConformingToTypeIdentifier:@"public.movie"]) {
|
||||
[itemProvider loadFileRepresentationForTypeIdentifier:@"public.movie" completionHandler:^(NSURL *videoURL, NSError *error) {
|
||||
if (error) {
|
||||
NSLog(@"Fehler beim Laden des Videos: %@", error);
|
||||
MXLogDebug(@"Uploading video from photo library failed: %@", error);
|
||||
} else {
|
||||
NSLog(@"Video URL: %@", [videoURL absoluteString]);
|
||||
NSString *dummy = [videoURL absoluteString];
|
||||
if(videoURL) {
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
AVURLAsset *selectedVideo = [AVURLAsset assetWithURL:videoURL];
|
||||
[self sendVideoAsset:selectedVideo isPhotoLibraryAsset:NO];
|
||||
});
|
||||
}
|
||||
// NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
|
||||
|
||||
// Hier können Sie mit dem geladenen videoData arbeiten
|
||||
// z.B. es in einem AVPlayer anzeigen oder auf einen Server hochladen
|
||||
// First make a copy of the video in the temp directory because the video at videoURL
|
||||
// is a copy by the iOS photo picker and will be dismissed when this completion block ends
|
||||
NSURL *videoCopy = [self makeVideoCopy:videoURL];
|
||||
[self uploadVideo:videoCopy];
|
||||
}
|
||||
}];
|
||||
// [itemProvider loadItemForTypeIdentifier:(NSString *)UTTypeMovie options:nil completionHandler:^(id _Nullable object, NSError * _Nullable error) {
|
||||
// dispatch_async(dispatch_get_main_queue(), ^{
|
||||
// if ([object isKindOfClass:[NSURL class]]) {
|
||||
// NSURL *url = object;
|
||||
// if(url) {
|
||||
// AVURLAsset *selectedVideo = [AVURLAsset assetWithURL:url];
|
||||
// [self sendVideoAsset:selectedVideo isPhotoLibraryAsset:NO];
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)uploadImage:(UIImage *)image {
|
||||
[self createDiscussionIfNeeded:^(BOOL readyToSend) {
|
||||
if (readyToSend && [self inputToolbarConformsToToolbarViewProtocol])
|
||||
{
|
||||
NSData *imageData = [image dataForPNGRepresentation];
|
||||
if(imageData) {
|
||||
[self.inputToolbarView sendSelectedImage:imageData
|
||||
withMimeType:@"image/png"
|
||||
andCompressionMode:MediaCompressionHelper.defaultCompressionMode
|
||||
isPhotoLibraryAsset:YES];
|
||||
} else {
|
||||
MXLogDebug(@"Uploading photo from photo library failed because imageData is nil")
|
||||
}
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)uploadVideo:(NSURL *)url {
|
||||
if(url) {
|
||||
NSLog(@"uploadVideo: %@", [url absoluteString]);
|
||||
|
||||
AVURLAsset *asset = [AVURLAsset assetWithURL:url];
|
||||
if(asset) {
|
||||
[self sendVideoAssetWithoutCompression:asset isPhotoLibraryAsset:YES];
|
||||
} else {
|
||||
MXLogDebug(@"Uploading video from photo library failed because asset is nil")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSURL *)makeVideoCopy:(NSURL *)sourceURL {
|
||||
if (sourceURL == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString *sourceFilename = [sourceURL lastPathComponent];
|
||||
|
||||
NSUUID *uuid = [NSUUID UUID];
|
||||
NSString *uuidString = [uuid UUIDString];
|
||||
|
||||
NSString *destinationFilename = [NSString stringWithFormat:@"%@-%@", uuidString, sourceFilename];
|
||||
|
||||
NSURL *tempDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
|
||||
NSURL *destinationURL = [tempDirURL URLByAppendingPathComponent:destinationFilename];
|
||||
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSError *error = nil;
|
||||
if ([fileManager copyItemAtURL:sourceURL toURL:destinationURL error:&error]) {
|
||||
MXLogDebug(@"Successfully copied video to a temp destination %@", [destinationURL absoluteString]);
|
||||
return destinationURL;
|
||||
} else {
|
||||
MXLogDebug(@"Failed to make a temp copy of the video file: %@", error);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)deleteFileAtURL:(NSURL *)url {
|
||||
if (url == nil) {
|
||||
return;
|
||||
}
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSError *error = nil;
|
||||
if (![fileManager removeItemAtURL:url error:&error]) {
|
||||
MXLogDebug(@"Cannot delete file at %@", [url absoluteString])
|
||||
MXLogDebug(@"%@", [error localizedDescription])
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - RoomCreationModalCoordinatorBridgePresenter
|
||||
|
||||
- (void)roomCreationModalCoordinatorBridgePresenterDelegateDidComplete:(RoomCreationModalCoordinatorBridgePresenter *)coordinatorBridgePresenter
|
||||
|
||||
Reference in New Issue
Block a user