Skip to content

Commit

Permalink
send images as sticker not only when they have the 140x140 dimensions…
Browse files Browse the repository at this point in the history
…, but also when they have transparent corner(s)

also add the send as sticker logic to darg and droping an image into a chat, this makes it super convinient to create stickers from images with iOS's AI selection feature.
(long press to select your favorite pet or person then hold the object and use a second finger to navigate to the chat you want to send it to.)
  • Loading branch information
Simon-Laux committed Aug 2, 2024
1 parent ee3ea19 commit 2c76616
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
35 changes: 35 additions & 0 deletions DcCore/DcCore/Extensions/UIImage+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,40 @@ public extension UIImage {
guard let alpha: CGImageAlphaInfo = self.cgImage?.alphaInfo else { return false }
return alpha == .first || alpha == .last || alpha == .premultipliedFirst || alpha == .premultipliedLast
}

func hasTransparentCorner() -> Bool {
if !self.isTransparent() {
return false
}
guard let cgImage = self.cgImage,
let data = cgImage.dataProvider?.data as Data?,
let dataPtr = data.withUnsafeBytes({ $0.bindMemory(to: UInt8.self).baseAddress }),
!data.isEmpty else {
return false // Unable to get the CGImage or image data
}

let width = cgImage.width
let height = cgImage.height
let bytesPerRow = cgImage.bytesPerRow

// Check the alpha values of the pixels at the corners
let topLeftIndex = 0
let topRightIndex = max(0, width - 1)
let bottomLeftIndex = max(0, bytesPerRow * (height - 1))
let bottomRightIndex = max(0, bytesPerRow * (height - 1) + width - 1)

var hasTransparentCorner = false
if dataPtr[topLeftIndex] < 255 {
hasTransparentCorner = true
} else if dataPtr[topRightIndex] < 255 {
hasTransparentCorner = true
} else if dataPtr[bottomLeftIndex] < 255 {
hasTransparentCorner = true
} else if dataPtr[min(data.count - 1, bottomRightIndex)] < 255 {
hasTransparentCorner = true
}

return hasTransparentCorner
}

}
10 changes: 7 additions & 3 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2760,8 +2760,7 @@ extension ChatViewController: UITextViewDelegate {
// MARK: - ChatInputTextViewPasteDelegate
extension ChatViewController: ChatInputTextViewPasteDelegate {
func onImagePasted(image: UIImage) {
let isSticker = image.size.equalTo(CGSize(width: 140, height: 140))

let isSticker = image.size.equalTo(CGSize(width: 140, height: 140)) || image.hasTransparentCorner()
if isSticker {
sendSticker(image)
} else {
Expand Down Expand Up @@ -2800,7 +2799,12 @@ extension ChatViewController: WebxdcSelectorDelegate {
// MARK: - ChatDropInteractionDelegate
extension ChatViewController: ChatDropInteractionDelegate {
func onImageDragAndDropped(image: UIImage) {
stageImage(image)
let isSticker = image.size.equalTo(CGSize(width: 140, height: 140)) || image.hasTransparentCorner()
if isSticker {
sendSticker(image)
} else {
stageImage(image)
}
}

func onVideoDragAndDropped(url: NSURL) {
Expand Down

0 comments on commit 2c76616

Please sign in to comment.