From 90379520c757bf39028db8ec927667e6aba87dda Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 28 May 2020 15:55:25 +0300 Subject: [PATCH] Video editor fixes --- .../LegacyComponents/Sources/PGPhotoEditor.m | 10 ++- .../TGMediaPickerGalleryVideoItemView.m | 69 ++++++++++++------- .../Sources/TGMediaVideoConverter.m | 14 +++- .../Sources/TGPhotoEditorTabController.m | 2 +- 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/submodules/LegacyComponents/Sources/PGPhotoEditor.m b/submodules/LegacyComponents/Sources/PGPhotoEditor.m index 4a4e0e58de..b22e95a3db 100644 --- a/submodules/LegacyComponents/Sources/PGPhotoEditor.m +++ b/submodules/LegacyComponents/Sources/PGPhotoEditor.m @@ -163,6 +163,11 @@ _fullSize = fullSize; } +- (CGFloat)_cropRectEpsilon +{ + return MAX(_originalSize.width, _originalSize.height) * 0.005f; +} + - (void)setPlayerItem:(AVPlayerItem *)playerItem forCropRect:(CGRect)cropRect cropRotation:(CGFloat)cropRotation cropOrientation:(UIImageOrientation)cropOrientation cropMirrored:(bool)cropMirrored { [_toolComposer invalidate]; _currentProcessChain = nil; @@ -171,13 +176,14 @@ PGVideoMovie *movie = [[PGVideoMovie alloc] initWithPlayerItem:playerItem]; _currentInput = movie; - bool hasCropping = !CGPointEqualToPoint(cropRect.origin, CGPointZero) || (!CGSizeEqualToSize(cropRect.size, CGSizeZero) && !CGSizeEqualToSize(cropRect.size, _originalSize)); + CGRect defaultCropRect = CGRectMake(0, 0, _originalSize.width, _originalSize.height); + bool hasCropping = !_CGRectEqualToRectWithEpsilon(self.cropRect, CGRectZero, [self _cropRectEpsilon]) && !_CGRectEqualToRectWithEpsilon(self.cropRect, defaultCropRect, [self _cropRectEpsilon]); _rotationMode = kGPUImageNoRotation; if (cropOrientation != UIImageOrientationUp || cropMirrored || hasCropping) { CGRect normalizedCropRect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); if (hasCropping) { - normalizedCropRect = CGRectMake(cropRect.origin.x / _originalSize.width, cropRect.origin.y / _originalSize.height, cropRect.size.width / _originalSize.width, cropRect.size.height / _originalSize.height); + normalizedCropRect = CGRectMake(MAX(0.0, MIN(1.0, cropRect.origin.x / _originalSize.width)), MAX(0.0, MIN(1.0, cropRect.origin.y / _originalSize.height)), MAX(0.0, MIN(1.0, cropRect.size.width / _originalSize.width)), MAX(0.0, MIN(1.0, cropRect.size.height / _originalSize.height))); } _cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:normalizedCropRect]; if (cropOrientation != UIImageOrientationUp || cropMirrored) { diff --git a/submodules/LegacyComponents/Sources/TGMediaPickerGalleryVideoItemView.m b/submodules/LegacyComponents/Sources/TGMediaPickerGalleryVideoItemView.m index b7dd171781..791c7c66b9 100644 --- a/submodules/LegacyComponents/Sources/TGMediaPickerGalleryVideoItemView.m +++ b/submodules/LegacyComponents/Sources/TGMediaPickerGalleryVideoItemView.m @@ -43,7 +43,7 @@ @interface TGMediaPickerGalleryVideoItemView() { UIView *_containerView; - TGModernGalleryVideoContentView *_contentView; + TGModernGalleryVideoContentView *_videoContentView; UIView *_playerWrapperView; UIView *_playerView; UIView *_playerContainerView; @@ -75,6 +75,7 @@ PGPhotoEditor *_photoEditor; UIImageView *_paintingImageView; + UIView *_contentView; UIView *_contentWrapperView; TGPhotoEntitiesContainerView *_entitiesContainerView; @@ -131,11 +132,11 @@ _containerView.clipsToBounds = true; [self addSubview:_containerView]; - _contentView = [[TGModernGalleryVideoContentView alloc] init]; - [_containerView addSubview:_contentView]; + _videoContentView = [[TGModernGalleryVideoContentView alloc] init]; + [_containerView addSubview:_videoContentView]; _playerWrapperView = [[UIView alloc] init]; - [_contentView addSubview:_playerWrapperView]; + [_videoContentView addSubview:_playerWrapperView]; _playerView = [[UIView alloc] init]; _playerView.clipsToBounds = true; @@ -150,8 +151,11 @@ _paintingImageView = [[UIImageView alloc] init]; [_playerContainerView addSubview:_paintingImageView]; + _contentView = [[UIView alloc] init]; + [_playerContainerView addSubview:_contentView]; + _contentWrapperView = [[UIView alloc] init]; - [_playerContainerView addSubview:_contentWrapperView]; + [_contentView addSubview:_contentWrapperView]; _entitiesContainerView = [[TGPhotoEntitiesContainerView alloc] init]; _entitiesContainerView.hidden = true; @@ -162,7 +166,7 @@ _curtainView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _curtainView.backgroundColor = [UIColor blackColor]; _curtainView.hidden = true; - [_contentView addSubview:_curtainView]; + [_videoContentView addSubview:_curtainView]; _actionButton = [[TGModernButton alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; _actionButton.modernHighlight = true; @@ -190,8 +194,8 @@ [_actionButton addTarget:self action:@selector(playPressed) forControlEvents:UIControlEventTouchUpInside]; - _contentView.button = _actionButton; - [_contentView addSubview:_actionButton]; + _videoContentView.button = _actionButton; + [_videoContentView addSubview:_actionButton]; TGMediaPickerScrubberHeaderView *headerView = [[TGMediaPickerScrubberHeaderView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 44)]; _headerView = headerView; @@ -223,7 +227,7 @@ _fileInfoLabel.textColor = [UIColor whiteColor]; _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)]; - [_contentView addGestureRecognizer:_tapGestureRecognizer]; + [_videoContentView addGestureRecognizer:_tapGestureRecognizer]; } return self; } @@ -696,7 +700,7 @@ [self _layoutPlayerView]; - _contentView.frame = (CGRect){CGPointZero, frame.size}; + _videoContentView.frame = (CGRect){CGPointZero, frame.size}; if (_tooltipContainerView != nil && frame.size.width > frame.size.height) { @@ -740,37 +744,52 @@ if (CGSizeEqualToSize(videoFrameSize, CGSizeZero)) return; - - CGSize fittedSize = TGScaleToSize(videoFrameSize, self.frame.size); - _playerWrapperView.frame = CGRectMake((_containerView.frame.size.width - fittedSize.width) / 2, (_containerView.frame.size.height - fittedSize.height) / 2, fittedSize.width, fittedSize.height); + + CGSize fittedVideoSize = TGScaleToSize(videoFrameSize, self.frame.size); + _playerWrapperView.frame = CGRectMake((_containerView.frame.size.width - fittedVideoSize.width) / 2, (_containerView.frame.size.height - fittedVideoSize.height) / 2, fittedVideoSize.width, fittedVideoSize.height); _playerView.frame = _playerWrapperView.bounds; _playerContainerView.frame = _playerView.bounds; - CGFloat ratio = fittedSize.width / videoFrameSize.width; + CGFloat ratio = fittedVideoSize.width / videoFrameSize.width; _imageView.frame = CGRectMake(-cropRect.origin.x * ratio, -cropRect.origin.y * ratio, _videoDimensions.width * ratio, _videoDimensions.height * ratio); _paintingImageView.frame = _imageView.frame; _videoView.frame = _imageView.frame; - CGRect rect = [TGPhotoPaintController fittedCropRect:cropRect originalSize:_videoDimensions keepOriginalSize:true]; + + CGSize originalSize = self.item.asset.originalSize; + + CGSize rotatedCropSize = cropRect.size; + if (orientation == UIImageOrientationLeft || orientation == UIImageOrientationRight) + rotatedCropSize = CGSizeMake(rotatedCropSize.height, rotatedCropSize.width); + + CGSize containerSize = _imageView.frame.size; + CGSize fittedSize = TGScaleToSize(rotatedCropSize, containerSize); + CGRect previewFrame = CGRectMake((containerSize.width - fittedSize.width) / 2, (containerSize.height - fittedSize.height) / 2, fittedSize.width, fittedSize.height); + + CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(TGRotationForOrientation(orientation)); + _contentView.transform = rotationTransform; + _contentView.frame = previewFrame; + + CGSize fittedContentSize = [TGPhotoPaintController fittedContentSize:cropRect orientation:orientation originalSize:originalSize]; + CGRect fittedCropRect = [TGPhotoPaintController fittedCropRect:cropRect originalSize:originalSize keepOriginalSize:false]; + _contentWrapperView.frame = CGRectMake(0.0f, 0.0f, fittedContentSize.width, fittedContentSize.height); + + CGFloat contentScale = _contentView.bounds.size.width / fittedCropRect.size.width; + _contentWrapperView.transform = CGAffineTransformMakeScale(contentScale, contentScale); + _contentWrapperView.frame = CGRectMake(0.0f, 0.0f, _contentView.bounds.size.width, _contentView.bounds.size.height); + + CGRect rect = [TGPhotoPaintController fittedCropRect:cropRect originalSize:originalSize keepOriginalSize:true]; _entitiesContainerView.frame = CGRectMake(0, 0, rect.size.width, rect.size.height); - CGSize fittedOriginalSize = TGScaleToSize(_videoDimensions, [TGPhotoPaintController maximumPaintingSize]); + CGSize fittedOriginalSize = TGScaleToSize(originalSize, [TGPhotoPaintController maximumPaintingSize]); CGSize rotatedSize = fittedOriginalSize; CGPoint centerPoint = CGPointMake(rotatedSize.width / 2.0f, rotatedSize.height / 2.0f); - CGFloat scale = fittedOriginalSize.width / _videoDimensions.width; + CGFloat scale = fittedOriginalSize.width / originalSize.width; CGPoint offset = TGPaintSubtractPoints(centerPoint, [TGPhotoPaintController fittedCropRect:cropRect centerScale:scale]); CGPoint boundsCenter = TGPaintCenterOfRect(_contentWrapperView.bounds); _entitiesContainerView.center = TGPaintAddPoints(boundsCenter, offset); - - CGSize fittedContentSize = [TGPhotoPaintController fittedContentSize:cropRect orientation:UIImageOrientationUp originalSize:_videoDimensions]; - CGRect fittedCropRect = [TGPhotoPaintController fittedCropRect:cropRect originalSize:_videoDimensions keepOriginalSize:false]; - _contentWrapperView.frame = CGRectMake(0.0f, 0.0f, fittedContentSize.width, fittedContentSize.height); - - CGFloat contentScale = _playerWrapperView.bounds.size.width / fittedCropRect.size.width; - _contentWrapperView.transform = CGAffineTransformMakeScale(contentScale, contentScale); - _contentWrapperView.frame = CGRectMake(0.0f, 0.0f, _playerWrapperView.bounds.size.width, _playerWrapperView.bounds.size.height); } - (void)singleTap diff --git a/submodules/LegacyComponents/Sources/TGMediaVideoConverter.m b/submodules/LegacyComponents/Sources/TGMediaVideoConverter.m index 0cc332392a..cace4686f5 100644 --- a/submodules/LegacyComponents/Sources/TGMediaVideoConverter.m +++ b/submodules/LegacyComponents/Sources/TGMediaVideoConverter.m @@ -380,12 +380,20 @@ videoComposition = [AVMutableVideoComposition videoCompositionWithAsset:avAsset applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest * _Nonnull request) { CIImage *resultImage = request.sourceImage; + CGSize size; if (backgroundCIImage != nil) { resultImage = backgroundCIImage; + size = backgroundCIImage.extent.size; + } else if ([adjustments cropAppliedForAvatar:false]) { + CGRect cropRect = adjustments.cropRect; + cropRect = CGRectMake(cropRect.origin.x, size.height - cropRect.size.height - cropRect.origin.y, cropRect.size.width, cropRect.size.height); + resultImage = [resultImage imageByCroppingToRect:cropRect]; + size = resultImage.extent.size; + } else { + size = resultImage.extent.size; } - + void (^process)(CIImage *, void(^)(void)) = ^(CIImage *resultImage, void(^unlock)(void)) { - CGSize size = resultImage.extent.size; if (overlayImage != nil && overlayImage.size.width > 0.0) { if (overlayCIImage == nil) { overlayCIImage = [[CIImage alloc] initWithImage:overlayImage]; @@ -410,7 +418,7 @@ } }; - if (editor != nil) { + if (editor != nil && backgroundCIImage == nil) { [editor setCIImage:resultImage]; [editor currentResultCIImage:^(CIImage *image, void(^unlock)(void)) { process(image, unlock); diff --git a/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m b/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m index af30edec6b..1c94714efb 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m +++ b/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m @@ -128,7 +128,7 @@ const CGFloat TGPhotoEditorToolbarSize = 49.0f; else { _transitionView = [referenceView snapshotViewAfterScreenUpdates:false]; - transitionViewSuperview = self.view; + transitionViewSuperview = parentView; }