From ad88356d7fd1a45430a34b1111c095972ebb8efc Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Mon, 13 Jul 2020 12:37:12 +0300 Subject: [PATCH] Video avatar fixes --- .../Sources/PhotoResources.swift | 54 +++++++++++-------- .../Sources/PeerInfo/PeerInfoScreen.swift | 51 ++++++++++-------- 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/submodules/PhotoResources/Sources/PhotoResources.swift b/submodules/PhotoResources/Sources/PhotoResources.swift index 09ce16b6f3..a2f8bff4c7 100644 --- a/submodules/PhotoResources/Sources/PhotoResources.swift +++ b/submodules/PhotoResources/Sources/PhotoResources.swift @@ -2210,31 +2210,41 @@ public func chatAvatarGalleryPhoto(account: Account, representations: [ImageRepr var blurredThumbnailImage: UIImage? if let thumbnailImage = thumbnailImage { - let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height) - - let initialThumbnailContextFittingSize = fittedSize.fitted(CGSize(width: 350.0, height: 350.0)) - - let thumbnailContextSize = thumbnailSize.aspectFilled(initialThumbnailContextFittingSize) - let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0) - thumbnailContext.withFlippedContext { c in - c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize)) - } - imageFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) - - blurredThumbnailImage = thumbnailContext.generateImage() - } - - if let blurredThumbnailImage = blurredThumbnailImage, fullSizeImage == nil { - let context = DrawingContext(size: blurredThumbnailImage.size, scale: blurredThumbnailImage.scale, clear: true) - context.withFlippedContext { c in - c.setBlendMode(.copy) - if let cgImage = blurredThumbnailImage.cgImage { + if max(thumbnailImage.width, thumbnailImage.height) > 200 { + blurredThumbnailImage = UIImage(cgImage: thumbnailImage) + } else { + let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height) + + let initialThumbnailContextFittingSize = fittedSize.fitted(CGSize(width: 100.0, height: 100.0)) + + let thumbnailContextSize = thumbnailSize.aspectFitted(initialThumbnailContextFittingSize) + let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0, clear: false) + thumbnailContext.withFlippedContext { c in c.interpolationQuality = .none - drawImage(context: c, image: cgImage, orientation: imageOrientation, in: CGRect(origin: CGPoint(), size: blurredThumbnailImage.size)) - c.setBlendMode(.normal) + c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize)) + } + imageFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) + + var thumbnailContextFittingSize = CGSize(width: floor(arguments.drawingSize.width * 0.5), height: floor(arguments.drawingSize.width * 0.5)) + if thumbnailContextFittingSize.width < 150.0 || thumbnailContextFittingSize.height < 150.0 { + thumbnailContextFittingSize = thumbnailContextFittingSize.aspectFilled(CGSize(width: 150.0, height: 150.0)) + } + + if thumbnailContextFittingSize.width > thumbnailContextSize.width { + let additionalContextSize = thumbnailContextFittingSize + let additionalBlurContext = DrawingContext(size: additionalContextSize, scale: 1.0, clear: false) + additionalBlurContext.withFlippedContext { c in + c.interpolationQuality = .default + if let image = thumbnailContext.generateImage()?.cgImage { + c.draw(image, in: CGRect(origin: CGPoint(), size: additionalContextSize)) + } + } + imageFastBlur(Int32(additionalContextSize.width), Int32(additionalContextSize.height), Int32(additionalBlurContext.bytesPerRow), additionalBlurContext.bytes) + blurredThumbnailImage = additionalBlurContext.generateImage() + } else { + blurredThumbnailImage = thumbnailContext.generateImage() } } - return context } let context = DrawingContext(size: arguments.drawingSize, clear: true) diff --git a/submodules/TelegramUI/Sources/PeerInfo/PeerInfoScreen.swift b/submodules/TelegramUI/Sources/PeerInfo/PeerInfoScreen.swift index 1546c2f1b4..baa186cdac 100644 --- a/submodules/TelegramUI/Sources/PeerInfo/PeerInfoScreen.swift +++ b/submodules/TelegramUI/Sources/PeerInfo/PeerInfoScreen.swift @@ -633,20 +633,17 @@ private func settingsItems(data: PeerInfoScreenData?, context: AccountContext, p items[section] = [] } - if let peer = data.peer { - if peer.smallProfileImage == nil { - items[.edit]!.append(PeerInfoScreenActionItem(id: 0, text: presentationData.strings.Settings_SetProfilePhotoOrVideo, icon: UIImage(bundleImageName: "Settings/SetAvatar"), action: { - interaction.openSettings(.avatar) - })) - } else if peer.addressName == nil { - items[.edit]!.append(PeerInfoScreenActionItem(id: 1, text: presentationData.strings.Settings_SetUsername, icon: UIImage(bundleImageName: "Settings/SetUsername"), action: { - interaction.openSettings(.username) - })) - } - } - items[.edit]!.append(PeerInfoScreenActionItem(id: 2, text: presentationData.strings.Settings_EditAccount, icon: UIImage(bundleImageName: "Settings/EditAccount"), action: { - interaction.openSettings(.edit) + items[.edit]!.append(PeerInfoScreenActionItem(id: 0, text: presentationData.strings.Settings_SetProfilePhotoOrVideo, icon: UIImage(bundleImageName: "Settings/SetAvatar"), action: { + interaction.openSettings(.avatar) })) + if let peer = data.peer, peer.addressName == nil { + items[.edit]!.append(PeerInfoScreenActionItem(id: 1, text: presentationData.strings.Settings_SetUsername, icon: UIImage(bundleImageName: "Settings/SetUsername"), action: { + interaction.openSettings(.username) + })) + } +// items[.edit]!.append(PeerInfoScreenActionItem(id: 2, text: presentationData.strings.Settings_EditAccount, icon: UIImage(bundleImageName: "Settings/EditAccount"), action: { +// interaction.openSettings(.edit) +// })) if let settings = data.globalSettings { @@ -4211,7 +4208,20 @@ private final class PeerInfoScreenNode: ViewControllerTracingNode, UIScrollViewD hasPhotos = true } + let paintStickersContext = LegacyPaintStickersContext(context: strongSelf.context) + paintStickersContext.presentStickersController = { completion in + let controller = DrawingStickersScreen(context: strongSelf.context, selectSticker: { fileReference, node, rect in + let coder = PostboxEncoder() + coder.encodeRootObject(fileReference.media) + completion?(coder.makeData(), fileReference.media.isAnimatedSticker, node.view, rect) + return true + }) + strongSelf.controller?.present(controller, in: .window(.root)) + return controller + } + let mixin = TGMediaAvatarMenuMixin(context: legacyController.context, parentController: emptyController, hasSearchButton: true, hasDeleteButton: hasPhotos && hasRemove, hasViewButton: false, personalPhoto: strongSelf.isSettings, isVideo: currentIsVideo, saveEditedPhotos: false, saveCapturedMedia: false, signup: false)! + mixin.stickersContext = paintStickersContext let _ = strongSelf.currentAvatarMixin.swap(mixin) mixin.requestSearchController = { [weak self] assetsController in guard let strongSelf = self else { @@ -5280,7 +5290,8 @@ private final class PeerInfoScreenNode: ViewControllerTracingNode, UIScrollViewD let effectiveAreaExpansionFraction: CGFloat if self.isSettings { - effectiveAreaExpansionFraction = self.headerNode.isAvatarExpanded ? 1.0 : 0.0 + effectiveAreaExpansionFraction = 0.0 +// effectiveAreaExpansionFraction = self.headerNode.isAvatarExpanded ? 1.0 : 0.0 } else if self.state.isEditing { effectiveAreaExpansionFraction = 0.0 } else { @@ -5308,12 +5319,8 @@ private final class PeerInfoScreenNode: ViewControllerTracingNode, UIScrollViewD navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .done, isForExpandedView: false)) } else { if self.isSettings { - navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .search, isForExpandedView: false)) - if let item = self.headerNode.avatarListNode.listContainerNode.currentItemNode?.item, case let .image(image) = item, !image.2.isEmpty { - navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .editVideo, isForExpandedView: true)) - } else { - navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .editPhoto, isForExpandedView: true)) - } + navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .edit, isForExpandedView: false)) + navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .search, isForExpandedView: true)) } else if peerInfoCanEdit(peer: self.data?.peer, cachedData: self.data?.cachedData) { navigationButtons.append(PeerInfoHeaderNavigationButtonSpec(key: .edit, isForExpandedView: false)) } @@ -5839,7 +5846,9 @@ public final class PeerInfoScreen: ViewController { super.didMove(toParent: viewController) if self.isSettings && viewController == nil { - self.controllerNode.resetHeaderExpansion() + Queue.mainQueue().after(0.1) { + self.controllerNode.resetHeaderExpansion() + } } }