質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

911閲覧

設定したimageviewが横向き、または上下反転します

KaoruYoshida

総合スコア36

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2022/10/30 06:50

編集2022/10/30 06:52

前提

navigation遷移した先のsettingviewで設定した写真を渡して
元の画面に表示するコードを書いています。

実現したいこと

imageviewの向きを元の画像のままに固定したいです。

発生している問題・エラーメッセージ

settingviewで設定する際は問題ないのですが、

イメージ説明

confirmボタンを押して元の画面に戻ると、このように上下反転したり横向きになったりします。

イメージ説明

画面の向きを元の画像のままに固定する、というようなメソッドなどあれば教えていただきたいです。

該当のソースコード

SettingView

1import SwiftUI 2 3struct SettingView: View { 4 @State private var profile_image = UIImage() 5 @State private var is_show_photo_library = false 6 7 @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> 8 @State var onSwitch = false 9 @Binding var bindImage: Data? 10 11 var body: some View { 12 VStack{ 13 Form { 14 VStack { 15 Image(uiImage: self.profile_image) 16 .resizable() 17 .clipShape(Circle()) 18 .overlay(Circle().stroke(Color.white, lineWidth: 2)) 19 .frame(width: 100, height: 100) 20 .shadow(radius: 5) 21 .scaledToFill() 22 .cornerRadius(75) 23 HStack { 24 Spacer() 25 26 Button(action: { 27 self.is_show_photo_library = true 28 }, label: { 29 Text("Please choose your profile photo") 30 .padding() 31 }) 32 33 Spacer() 34 } 35 } 36 .sheet(isPresented: $is_show_photo_library, content: { 37 ImagePicker(sourceType: .photoLibrary, selectedImage: self.$profile_image) 38 }) 39 if (self.name.isEmpty){ 40 Button(action: { 41 42 }) { 43 Text("confirm") 44 .font(.title2) 45 .padding(16) 46 .foregroundColor(Color.white) 47 .cornerRadius(10) 48 }.disabled(self.name.isEmpty) 49 } else { 50 Button(action: { 51 bindName = name 52 bindType = "\(food_style)" 53 bindImage = profile_image.pngData() 54 UserDefaults.standard.set(self.bindName, forKey: "launchedName") 55 UserDefaults.standard.set(self.bindImage, forKey: "launchedImage") 56 self.onSwitch.toggle() 57 self.presentationMode.wrappedValue.dismiss() 58 }) { 59 Text("confirm") 60 .font(.title2) 61 .padding(16) 62 .background(Color(red: 82/225, green: 194/225, blue: 125/225)) 63 .foregroundColor(Color.white) 64 .cornerRadius(10) 65 } 66 } 67 }.navigationTitle("Settings") 68 } 69}

FirstView

1import SwiftUI 2import UIKit 3import RiveRuntime 4 5struct FirstView: View { 6 @State var showsheet: Bool = false 7 @State var showinfo: Bool = false 8 9 @Environment(\.isPresented) var isPresented 10 @Environment(\.dismiss) var dismiss 11 12 @AppStorage("IntegerKey") var day = "0" 13 @AppStorage("launchedImage") var bindImage: Data? 14 15 @State private var isShowSubView = false 16 17 var body: some View { 18 let bounds = UIScreen.main.bounds 19 NavigationView{ 20 ZStack{ 21 Image("background") 22 .ignoresSafeArea() 23 .blur(radius: 30) 24 ZStack { 25 VStack { 26 VStack(spacing: bounds.height / 70){ 27 if let image = bindImage, 28 let uiimage = UIImage(data: image){ 29 Image(uiImage: uiimage) 30 .resizable() 31 .clipShape(Circle()) 32 .overlay(Circle().stroke(Color.white, lineWidth: 2)) 33 .frame(width: bounds.height / 7.5, height: bounds.height / 7.5) 34 .shadow(radius: 5) 35 .scaledToFill() 36 .cornerRadius(75) 37 } else { 38 Image("") 39 .resizable() 40 .clipShape(Circle()) 41 .overlay(Circle().stroke(Color.white, lineWidth: 2)) 42 .frame(width: bounds.height / 7.5, height: bounds.height / 7.5) 43 .shadow(radius: 5) 44 .scaledToFill() 45 .cornerRadius(75) 46 } 47 } 48 } 49 .frame(width: bounds.width / 1.2, height: bounds.height / 1.29) 50 .offset(y: -20) 51 .toolbar{ 52 ToolbarItemGroup(placement: .navigationBarTrailing) { 53 NavigationLink(destination: SettingView(bindName: $bindName, bindType: $bindType, bindImage: $bindImage)) { 54 Label("Setting", systemImage: "gearshape") 55 } 56 } 57 } 58 .foregroundColor(.black) 59 } 60 .background(.white) 61 .mask(RoundedRectangle(cornerRadius: 20, style: .continuous)) 62 .shadow(color: .gray.opacity(0.3), radius: 5, x: 0, y: 0) 63 .padding() 64 .offset(y: -35) 65 } 66 } 67 } 68} 69func passdate(date: Binding<Date>){ 70 return 71}

補足情報(FW/ツールのバージョンなど)

macOS バージョン12.3.1
2.6 GHz 6コアIntel Core i7
Xcodeバージョン14.0.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

PNGで保存しようとすると回転が変わってしまうみたいですね。
*JPEGだと大丈夫みたいですね
*アイコンのような色が少ない場合はPNGの方が良いみたいですが、写真のような色が多い場合はJPEGの方が良いみたいですね

If you need to save your PNG with correct rotation you will need to redraw your image if its orientation it is not .up. You can redraw it as follow:
(機械翻訳)PNGを正しい回転で保存する必要がある場合、画像の向きが.upでない場合は、再描画する必要があります。以下のように再描画してください。
https://stackoverflow.com/questions/42098390/swift-png-image-being-saved-with-incorrect-orientation/42098812

PNGで保存したい場合
画像の向きが.upでない場合は、向きを変えるように再描画してから保存すると良いみたいですね。
*リンク先のコードにある通りにすれば解消できると思いますが、不明な点がありましたらコメントくださいね

投稿2022/10/30 08:07

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KaoruYoshida

2022/10/30 12:44

いつも適切な回答くださりありがとうございます!! データ変換のところ、 bindImage = profile_image.png() から bindImage = profile_image.jpegData(compressionQuality: 1) に変えると直りました!感謝してます。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問