Fix issue with importing images from photos.
This commit is contained in:
parent
615b78c7af
commit
aa0ea05f45
|
@ -142,6 +142,8 @@
|
|||
F8C5E55F2988E92600ADF6A7 /* AccountModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8C5E55E2988E92600ADF6A7 /* AccountModel.swift */; };
|
||||
F8C5E56229892CC300ADF6A7 /* FirstAppear.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8C5E56129892CC300ADF6A7 /* FirstAppear.swift */; };
|
||||
F8CC95CE2970761D00C9C2AC /* TintColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8CC95CD2970761D00C9C2AC /* TintColor.swift */; };
|
||||
F8CEEDF829ABADDD00DBED66 /* UIImage+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8CEEDF729ABADDD00DBED66 /* UIImage+Size.swift */; };
|
||||
F8CEEDFA29ABAFD200DBED66 /* ImageFileTranseferable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8CEEDF929ABAFD200DBED66 /* ImageFileTranseferable.swift */; };
|
||||
F8FA9917299F7DBD007AB130 /* Client+Media.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FA9916299F7DBD007AB130 /* Client+Media.swift */; };
|
||||
F8FA9919299FA35A007AB130 /* PhotoAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FA9918299FA35A007AB130 /* PhotoAttachment.swift */; };
|
||||
F8FA991C299FA8C2007AB130 /* ImageUploadView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FA991B299FA8C2007AB130 /* ImageUploadView.swift */; };
|
||||
|
@ -284,6 +286,8 @@
|
|||
F8C5E56129892CC300ADF6A7 /* FirstAppear.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstAppear.swift; sourceTree = "<group>"; };
|
||||
F8C937A929882CA90004D782 /* Vernissage-001.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Vernissage-001.xcdatamodel"; sourceTree = "<group>"; };
|
||||
F8CC95CD2970761D00C9C2AC /* TintColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TintColor.swift; sourceTree = "<group>"; };
|
||||
F8CEEDF729ABADDD00DBED66 /* UIImage+Size.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Size.swift"; sourceTree = "<group>"; };
|
||||
F8CEEDF929ABAFD200DBED66 /* ImageFileTranseferable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageFileTranseferable.swift; sourceTree = "<group>"; };
|
||||
F8FA9916299F7DBD007AB130 /* Client+Media.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Client+Media.swift"; sourceTree = "<group>"; };
|
||||
F8FA9918299FA35A007AB130 /* PhotoAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhotoAttachment.swift; sourceTree = "<group>"; };
|
||||
F8FA991B299FA8C2007AB130 /* ImageUploadView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageUploadView.swift; sourceTree = "<group>"; };
|
||||
|
@ -382,6 +386,7 @@
|
|||
F8210DE62966E1D1001D9973 /* Color+Assets.swift */,
|
||||
F8C14393296AF21B001FE31D /* Double+Round.swift */,
|
||||
F8984E4C296B648000A2610F /* UIImage+Blurhash.swift */,
|
||||
F8CEEDF729ABADDD00DBED66 /* UIImage+Size.swift */,
|
||||
F8996DEA2971D29D0043EEC6 /* View+Transition.swift */,
|
||||
F88E4D43297E82EB0057491A /* Status+MediaAttachmentType.swift */,
|
||||
);
|
||||
|
@ -402,6 +407,7 @@
|
|||
F8764186298ABB520057D362 /* ViewState.swift */,
|
||||
F8FA9918299FA35A007AB130 /* PhotoAttachment.swift */,
|
||||
F89AC00429A1F9B500F4159F /* AppMetadata.swift */,
|
||||
F8CEEDF929ABAFD200DBED66 /* ImageFileTranseferable.swift */,
|
||||
);
|
||||
path = Models;
|
||||
sourceTree = "<group>";
|
||||
|
@ -827,6 +833,7 @@
|
|||
F83901A6295D8EC000456AE2 /* LabelIcon.swift in Sources */,
|
||||
F8FA991C299FA8C2007AB130 /* ImageUploadView.swift in Sources */,
|
||||
F8B1E6512973FB7E00EE0D10 /* ToastrService.swift in Sources */,
|
||||
F8CEEDF829ABADDD00DBED66 /* UIImage+Size.swift in Sources */,
|
||||
F88E4D48297E90CD0057491A /* TrendStatusesView.swift in Sources */,
|
||||
F89992CE296D92E7005994BF /* AttachmentModel.swift in Sources */,
|
||||
F800480A2961EA1900E6868A /* AttachmentDataHandler.swift in Sources */,
|
||||
|
@ -872,6 +879,7 @@
|
|||
F85E1320297409CD006A051D /* ErrorsService.swift in Sources */,
|
||||
F88C246C295C37B80006098B /* VernissageApp.swift in Sources */,
|
||||
F8121CA8298A86D600B466C7 /* InstanceRowView.swift in Sources */,
|
||||
F8CEEDFA29ABAFD200DBED66 /* ImageFileTranseferable.swift in Sources */,
|
||||
F802884F297AEED5000BDD51 /* DatabaseError.swift in Sources */,
|
||||
F86A4307299AA5E900DF7645 /* ThanksView.swift in Sources */,
|
||||
F85D4971296402DC00751DF7 /* AuthorizationService.swift in Sources */,
|
||||
|
@ -1027,7 +1035,7 @@
|
|||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_IDENTITY = "Apple Development";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 33;
|
||||
CURRENT_PROJECT_VERSION = 34;
|
||||
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = B2U9FEKYP8;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
|
@ -1064,7 +1072,7 @@
|
|||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 33;
|
||||
CURRENT_PROJECT_VERSION = 34;
|
||||
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = B2U9FEKYP8;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
//
|
||||
// https://mczachurski.dev
|
||||
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
||||
// Licensed under the MIT License.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIImage {
|
||||
func resized(to size: CGSize) -> UIImage {
|
||||
UIGraphicsImageRenderer(size: size).image { _ in
|
||||
draw(in: CGRect(origin: .zero, size: size))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
//
|
||||
// https://mczachurski.dev
|
||||
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
||||
// Licensed under the MIT License.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
public struct ImageFileTranseferable: Transferable {
|
||||
let url: URL
|
||||
lazy var data: Data? = try? Data(contentsOf: url)
|
||||
|
||||
public static var transferRepresentation: some TransferRepresentation {
|
||||
FileRepresentation(contentType: .image) { image in
|
||||
SentTransferredFile(image.url)
|
||||
} importing: { received in
|
||||
Self(url: localURLFor(received: received))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func localURLFor(received: ReceivedTransferredFile) -> URL {
|
||||
let copy = URL.temporaryDirectory.appending(path: "\(UUID().uuidString).\(received.file.pathExtension)")
|
||||
try? FileManager.default.copyItem(at: received.file, to: copy)
|
||||
return copy
|
||||
}
|
|
@ -7,6 +7,7 @@
|
|||
import SwiftUI
|
||||
import PhotosUI
|
||||
import PixelfedKit
|
||||
import UIKit
|
||||
|
||||
struct ComposeView: View {
|
||||
@EnvironmentObject var applicationState: ApplicationState
|
||||
|
@ -392,8 +393,8 @@ struct ComposeView: View {
|
|||
|
||||
// Now we have to get from photos images as JPEG.
|
||||
for item in self.photosAttachment.filter({ $0.photoData == nil }) {
|
||||
if let photoData = try await item.photosPickerItem.loadTransferable(type: Data.self) {
|
||||
item.photoData = photoData
|
||||
if var imageFileTransferable = try await item.photosPickerItem.loadTransferable(type: ImageFileTranseferable.self) {
|
||||
item.photoData = imageFileTransferable.data
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -425,12 +426,21 @@ struct ComposeView: View {
|
|||
|
||||
private func upload(_ photoAttachment: PhotoAttachment) async {
|
||||
do {
|
||||
// We have to have binary data and image shouldn't be uploaded yet.
|
||||
guard let photoData = photoAttachment.photoData, photoAttachment.uploadedAttachment == nil else {
|
||||
return
|
||||
}
|
||||
|
||||
guard let image = UIImage(data: photoData) else {
|
||||
return
|
||||
}
|
||||
|
||||
guard let data = self.getJpegData(image: image) else {
|
||||
return
|
||||
}
|
||||
|
||||
let fileIndex = String.randomString(length: 8)
|
||||
if let mediaAttachment = try await self.client.media?.upload(data: photoData,
|
||||
if let mediaAttachment = try await self.client.media?.upload(data: data,
|
||||
fileName: "file-\(fileIndex).jpg",
|
||||
mimeType: "image/jpeg") {
|
||||
photoAttachment.uploadedAttachment = mediaAttachment
|
||||
|
@ -441,6 +451,18 @@ struct ComposeView: View {
|
|||
}
|
||||
}
|
||||
|
||||
private func getJpegData(image: UIImage) -> Data? {
|
||||
// API don't support images over 5K.
|
||||
if image.size.height > 5000 || image.size.width > 5000 {
|
||||
return image
|
||||
.resized(to: .init(width: image.size.width / 4, height: image.size.height / 4))
|
||||
.jpegData(compressionQuality: 0.80)
|
||||
} else {
|
||||
return image
|
||||
.jpegData(compressionQuality: 0.80)
|
||||
}
|
||||
}
|
||||
|
||||
private func publishStatus() async {
|
||||
do {
|
||||
let status = self.createStatus()
|
||||
|
|
|
@ -16,7 +16,7 @@ struct PhotoEditorView: View {
|
|||
var body: some View {
|
||||
NavigationView {
|
||||
VStack(alignment: .leading) {
|
||||
if let photoData = photoAttachment.photoData, let uiImage = UIImage(data: photoData) {
|
||||
if let data = photoAttachment.photoData, let uiImage = UIImage(data: data) {
|
||||
List {
|
||||
Section(header: Text("Photo")) {
|
||||
HStack {
|
||||
|
|
Loading…
Reference in New Issue