Video avatar fixes

This commit is contained in:
Ilya Laktyushin
2020-07-16 17:07:16 +03:00
parent 2abe662fed
commit f041a7df2e
10 changed files with 71 additions and 76 deletions

View File

@@ -37,7 +37,7 @@ typedef enum
- (instancetype)editAdjustmentsWithPreset:(TGMediaVideoConversionPreset)preset maxDuration:(NSTimeInterval)maxDuration; - (instancetype)editAdjustmentsWithPreset:(TGMediaVideoConversionPreset)preset maxDuration:(NSTimeInterval)maxDuration;
- (instancetype)editAdjustmentsWithPreset:(TGMediaVideoConversionPreset)preset videoStartValue:(NSTimeInterval)videoStartValue trimStartValue:(NSTimeInterval)trimStartValue trimEndValue:(NSTimeInterval)trimEndValue; - (instancetype)editAdjustmentsWithPreset:(TGMediaVideoConversionPreset)preset videoStartValue:(NSTimeInterval)videoStartValue trimStartValue:(NSTimeInterval)trimStartValue trimEndValue:(NSTimeInterval)trimEndValue;
+ (instancetype)editAdjustmentsWithOriginalSize:(CGSize)originalSize preset:(TGMediaVideoConversionPreset)preset; + (instancetype)editAdjustmentsWithOriginalSize:(CGSize)originalSize preset:(TGMediaVideoConversionPreset)preset;
+ (instancetype)editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)values; + (instancetype)editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)values preset:(TGMediaVideoConversionPreset)preset;
+ (instancetype)editAdjustmentsWithDictionary:(NSDictionary *)dictionary; + (instancetype)editAdjustmentsWithDictionary:(NSDictionary *)dictionary;
+ (instancetype)editAdjustmentsWithOriginalSize:(CGSize)originalSize + (instancetype)editAdjustmentsWithOriginalSize:(CGSize)originalSize

View File

@@ -898,7 +898,7 @@ const NSUInteger TGAttachmentDisplayedAssetLimit = 500;
}; };
__weak TGPhotoEditorController *weakController = controller; __weak TGPhotoEditorController *weakController = controller;
controller.didFinishEditing = ^(__unused id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, __unused UIImage *thumbnailImage, __unused bool hasChanges) controller.didFinishEditing = ^(id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, __unused UIImage *thumbnailImage, __unused bool hasChanges)
{ {
if (!hasChanges) if (!hasChanges)
return; return;
@@ -911,8 +911,34 @@ const NSUInteger TGAttachmentDisplayedAssetLimit = 500;
if (strongController == nil) if (strongController == nil)
return; return;
if (strongSelf.avatarCompletionBlock != nil) if (adjustments.paintingData.hasAnimation) {
strongSelf.avatarCompletionBlock(resultImage); TGVideoEditAdjustments *videoAdjustments = adjustments;
if ([videoAdjustments isKindOfClass:[PGPhotoEditorValues class]]) {
videoAdjustments = [TGVideoEditAdjustments editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)adjustments preset:TGMediaVideoConversionPresetProfileVeryHigh];
}
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSString alloc] initWithFormat:@"gifvideo_%x.jpg", (int)arc4random()]];
NSData *data = UIImageJPEGRepresentation(resultImage, 0.8);
[data writeToFile:filePath atomically:true];
UIImage *previewImage = resultImage;
if ([adjustments cropAppliedForAvatar:false] || adjustments.hasPainting || adjustments.toolsApplied)
{
UIImage *paintingImage = adjustments.paintingData.stillImage;
if (paintingImage == nil) {
paintingImage = adjustments.paintingData.image;
}
UIImage *thumbnailImage = TGPhotoEditorVideoExtCrop(resultImage, paintingImage, adjustments.cropOrientation, adjustments.cropRotation, adjustments.cropRect, adjustments.cropMirrored, TGScaleToFill(asset.dimensions, CGSizeMake(800, 800)), adjustments.originalSize, true, true, true);
if (thumbnailImage != nil) {
previewImage = thumbnailImage;
}
}
if (strongSelf.avatarVideoCompletionBlock != nil)
strongSelf.avatarVideoCompletionBlock(previewImage, [NSURL fileURLWithPath:filePath], videoAdjustments);
} else {
if (strongSelf.avatarCompletionBlock != nil)
strongSelf.avatarCompletionBlock(resultImage);
}
}; };
controller.didFinishEditingVideo = ^(AVAsset *asset, id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, UIImage *thumbnailImage, bool hasChanges) { controller.didFinishEditingVideo = ^(AVAsset *asset, id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, UIImage *thumbnailImage, bool hasChanges) {
if (!hasChanges) if (!hasChanges)

View File

@@ -2592,7 +2592,7 @@ static CGPoint TGCameraControllerClampPointToScreenSize(__unused id self, __unus
if (animated) { if (animated) {
dict[@"isAnimation"] = @true; dict[@"isAnimation"] = @true;
if ([adjustments isKindOfClass:[PGPhotoEditorValues class]]) { if ([adjustments isKindOfClass:[PGPhotoEditorValues class]]) {
dict[@"adjustments"] = [TGVideoEditAdjustments editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)adjustments]; dict[@"adjustments"] = [TGVideoEditAdjustments editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)adjustments preset:TGMediaVideoConversionPresetAnimation];
} else { } else {
dict[@"adjustments"] = adjustments; dict[@"adjustments"] = adjustments;
} }

View File

@@ -904,7 +904,7 @@
if (animated) { if (animated) {
dict[@"isAnimation"] = @true; dict[@"isAnimation"] = @true;
if ([adjustments isKindOfClass:[PGPhotoEditorValues class]]) { if ([adjustments isKindOfClass:[PGPhotoEditorValues class]]) {
dict[@"adjustments"] = [TGVideoEditAdjustments editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)adjustments]; dict[@"adjustments"] = [TGVideoEditAdjustments editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)adjustments preset:TGMediaVideoConversionPresetAnimation];
} else { } else {
dict[@"adjustments"] = adjustments; dict[@"adjustments"] = adjustments;
} }

View File

@@ -415,7 +415,7 @@
[[strongSelf->_assetsLibrary saveAssetWithImage:resultImage] startWithNext:nil]; [[strongSelf->_assetsLibrary saveAssetWithImage:resultImage] startWithNext:nil];
}; };
controller.didFinishEditing = ^(__unused id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, __unused UIImage *thumbnailImage, bool hasChanges) controller.didFinishEditing = ^(id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, __unused UIImage *thumbnailImage, bool hasChanges)
{ {
if (!hasChanges) if (!hasChanges)
return; return;
@@ -424,7 +424,32 @@
if (strongSelf == nil) if (strongSelf == nil)
return; return;
[(TGMediaAssetsController *)strongSelf.navigationController completeWithAvatarImage:resultImage]; if (adjustments.paintingData.hasAnimation) {
TGVideoEditAdjustments *videoAdjustments = adjustments;
if ([videoAdjustments isKindOfClass:[PGPhotoEditorValues class]]) {
videoAdjustments = [TGVideoEditAdjustments editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)adjustments preset:TGMediaVideoConversionPresetProfileVeryHigh];
}
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSString alloc] initWithFormat:@"gifvideo_%x.jpg", (int)arc4random()]];
NSData *data = UIImageJPEGRepresentation(resultImage, 0.8);
[data writeToFile:filePath atomically:true];
UIImage *previewImage = resultImage;
if ([adjustments cropAppliedForAvatar:false] || adjustments.hasPainting || adjustments.toolsApplied)
{
UIImage *paintingImage = adjustments.paintingData.stillImage;
if (paintingImage == nil) {
paintingImage = adjustments.paintingData.image;
}
UIImage *thumbnailImage = TGPhotoEditorVideoExtCrop(resultImage, paintingImage, adjustments.cropOrientation, adjustments.cropRotation, adjustments.cropRect, adjustments.cropMirrored, TGScaleToFill(asset.dimensions, CGSizeMake(800, 800)), adjustments.originalSize, true, true, true);
if (thumbnailImage != nil) {
previewImage = thumbnailImage;
}
}
[(TGMediaAssetsController *)strongSelf.navigationController completeWithAvatarVideo:[NSURL fileURLWithPath:filePath] adjustments:videoAdjustments image:previewImage];
} else {
[(TGMediaAssetsController *)strongSelf.navigationController completeWithAvatarImage:resultImage];
}
}; };
controller.didFinishEditingVideo = ^(AVAsset *asset, id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, UIImage *thumbnailImage, bool hasChanges) { controller.didFinishEditingVideo = ^(AVAsset *asset, id<TGMediaEditAdjustments> adjustments, UIImage *resultImage, UIImage *thumbnailImage, bool hasChanges) {
if (!hasChanges) if (!hasChanges)

View File

@@ -1061,8 +1061,13 @@
UIImage *image = result[@"image"]; UIImage *image = result[@"image"];
UIImage *thumbnailImage = result[@"thumbnail"]; UIImage *thumbnailImage = result[@"thumbnail"];
if (avatar && completion != nil) if (avatar && image.size.width < 150.0) {
image = TGScaleImageToPixelSize(image, CGSizeMake(150.0, 150.0));
}
if (avatar && completion != nil) {
completion(image); completion(image);
}
if (!saveOnly && didFinishEditing != nil) if (!saveOnly && didFinishEditing != nil)
didFinishEditing(editorValues, image, thumbnailImage, true); didFinishEditing(editorValues, image, thumbnailImage, true);

View File

@@ -1900,6 +1900,8 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f;
- (void)transitionOutSwitching:(bool)__unused switching completion:(void (^)(void))completion - (void)transitionOutSwitching:(bool)__unused switching completion:(void (^)(void))completion
{ {
[_stickersScreen invalidate];
TGPhotoEditorPreviewView *previewView = self.previewView; TGPhotoEditorPreviewView *previewView = self.previewView;
previewView.interactionEnded = nil; previewView.interactionEnded = nil;
@@ -1931,9 +1933,7 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f;
- (void)_animatePreviewViewTransitionOutToFrame:(CGRect)targetFrame saving:(bool)saving parentView:(UIView *)parentView completion:(void (^)(void))completion - (void)_animatePreviewViewTransitionOutToFrame:(CGRect)targetFrame saving:(bool)saving parentView:(UIView *)parentView completion:(void (^)(void))completion
{ {
_dismissing = true; _dismissing = true;
[_stickersScreen invalidate];
[_entitySelectionView removeFromSuperview]; [_entitySelectionView removeFromSuperview];
_entitySelectionView = nil; _entitySelectionView = nil;

View File

@@ -136,7 +136,7 @@ const NSTimeInterval TGVideoEditMaximumGifDuration = 30.5;
return adjustments; return adjustments;
} }
+ (instancetype)editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)values { + (instancetype)editAdjustmentsWithPhotoEditorValues:(PGPhotoEditorValues *)values preset:(TGMediaVideoConversionPreset)preset {
TGVideoEditAdjustments *adjustments = [[[self class] alloc] init]; TGVideoEditAdjustments *adjustments = [[[self class] alloc] init];
adjustments->_originalSize = values.originalSize; adjustments->_originalSize = values.originalSize;
CGRect cropRect = values.cropRect; CGRect cropRect = values.cropRect;
@@ -150,7 +150,7 @@ const NSTimeInterval TGVideoEditMaximumGifDuration = 30.5;
adjustments->_cropMirrored = values.cropMirrored; adjustments->_cropMirrored = values.cropMirrored;
adjustments->_paintingData = [values.paintingData dataForAnimation]; adjustments->_paintingData = [values.paintingData dataForAnimation];
adjustments->_sendAsGif = true; adjustments->_sendAsGif = true;
adjustments->_preset = TGMediaVideoConversionPresetAnimation; adjustments->_preset = preset;
return adjustments; return adjustments;
} }

View File

@@ -3846,68 +3846,7 @@ private final class PeerInfoScreenNode: ViewControllerTracingNode, UIScrollViewD
} }
} }
} }
private func editAvatarItem(_ item: PeerInfoAvatarListItem) {
guard case let .image(reference, representations, videoRepresentations, _) = item else {
return
}
let mediaReference: AnyMediaReference
if let video = videoRepresentations.last {
mediaReference = .standalone(media: TelegramMediaFile(fileId: MediaId(namespace: Namespaces.Media.LocalFile, id: 0), partialReference: nil, resource: video.representation.resource, previewRepresentations: [], videoThumbnails: [], immediateThumbnailData: nil, mimeType: "video/mp4", size: nil, attributes: [.Animated, .Video(duration: 0, size: video.representation.dimensions, flags: [])]))
} else {
let media = TelegramMediaImage(imageId: MediaId(namespace: 0, id: 0), representations: representations.map({ $0.representation }), immediateThumbnailData: nil, reference: nil, partialReference: nil, flags: [])
mediaReference = .standalone(media: media)
}
var dismissStatus: (() -> Void)?
let statusController = OverlayStatusController(theme: self.presentationData.theme, type: .loading(cancelled: {
dismissStatus?()
}))
dismissStatus = { [weak self, weak statusController] in
self?.editAvatarDisposable.set(nil)
statusController?.dismiss()
}
self.controller?.present(statusController, in: .window(.root))
self.editAvatarDisposable.set((fetchMediaData(context: self.context, postbox: self.context.account.postbox, mediaReference: mediaReference)
|> deliverOnMainQueue).start(next: { [weak self] state, isImage in
guard let strongSelf = self else {
return
}
switch state {
case .progress:
break
case let .data(data):
dismissStatus?()
let image: UIImage?
let video: URL?
if isImage {
if let fileData = try? Data(contentsOf: URL(fileURLWithPath: data.path)) {
image = UIImage(data: fileData)
} else {
image = nil
}
video = nil
} else {
image = nil
video = URL(fileURLWithPath: data.path)
}
// presentLegacyAvatarEditor(theme: strongSelf.presentationData.theme, image: image, video: video, present: { [weak self] c, a in
// if let strongSelf = self {
// strongSelf.controller?.present(c, in: .window(.root), with: a, blockInteraction: true)
// }
// }, imageCompletion: { [weak self] image in
// self?.updateProfilePhoto(image)
// }, videoCompletion: { [weak self] image, url, adjustments in
// self?.updateProfileVideo(image, url: url, adjustments: adjustments)
// })
}
}))
}
private func setMainAvatar(_ item: PeerInfoAvatarListItem) { private func setMainAvatar(_ item: PeerInfoAvatarListItem) {
if self.data?.peer?.id == self.context.account.peerId { if self.data?.peer?.id == self.context.account.peerId {
if case let .image(reference, _, _, _) = item { if case let .image(reference, _, _, _) = item {

View File

@@ -443,7 +443,7 @@ public func legacyEnqueueWebSearchMessages(_ selectionState: TGMediaSelectionCon
if animated { if animated {
dict["isAnimation"] = true dict["isAnimation"] = true
if let photoEditorValues = adjustments as? PGPhotoEditorValues { if let photoEditorValues = adjustments as? PGPhotoEditorValues {
dict["adjustments"] = TGVideoEditAdjustments(photoEditorValues: photoEditorValues) dict["adjustments"] = TGVideoEditAdjustments(photoEditorValues: photoEditorValues, preset: TGMediaVideoConversionPresetAnimation)
} }
let filePath = NSTemporaryDirectory().appending("/gifvideo_\(arc4random()).jpg") let filePath = NSTemporaryDirectory().appending("/gifvideo_\(arc4random()).jpg")