-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Ditectrev/develop
Develop
- Loading branch information
Showing
7 changed files
with
364 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
CloudMaster/Features/Shared/Components/QuestionImages.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import SwiftUI | ||
|
||
struct QuestionImages: View { | ||
let images: [Question.ImageInfo] | ||
@Binding var currentImageIndex: Int | ||
@Binding var isFullscreenImageShown: Bool | ||
@Binding var selectedImageIndex: Int | ||
|
||
var body: some View { | ||
if !images.isEmpty { | ||
TabView(selection: $currentImageIndex) { | ||
ForEach(images.indices, id: \.self) { index in | ||
let imageInfo = images[index] | ||
if let image = loadImage(from: imageInfo.path) { | ||
Image(uiImage: image) | ||
.resizable() | ||
.cornerRadius(2) | ||
.aspectRatio(contentMode: .fit) | ||
.frame(maxWidth: .infinity) | ||
.padding(.horizontal) | ||
.tag(index) | ||
.onTapGesture { | ||
selectedImageIndex = index | ||
isFullscreenImageShown = true | ||
} | ||
} else if let url = imageInfo.url, let urlImage = loadImage(from: url) { | ||
Image(uiImage: urlImage) | ||
.resizable() | ||
.cornerRadius(2) | ||
.aspectRatio(contentMode: .fit) | ||
.frame(maxWidth: .infinity) | ||
.padding(.horizontal) | ||
.tag(index) | ||
.onTapGesture { | ||
selectedImageIndex = index | ||
isFullscreenImageShown = true | ||
} | ||
} | ||
} | ||
} | ||
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .always)) | ||
.frame(height: 300) | ||
} | ||
} | ||
|
||
private func loadImage(from imagePath: String) -> UIImage? { | ||
let fileManager = FileManager.default | ||
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! | ||
let imageURL = documentsURL.appendingPathComponent(imagePath) | ||
return UIImage(contentsOfFile: imageURL.path) | ||
} | ||
} | ||
|
||
|
||
struct FullscreenImageView: View { | ||
let images: [Question.ImageInfo] | ||
@Binding var selectedImageIndex: Int | ||
@Binding var isShown: Bool | ||
|
||
@State private var scale: CGFloat = 1.0 | ||
@State private var lastScale: CGFloat = 1.0 | ||
|
||
var body: some View { | ||
ZStack { | ||
Color.black.opacity(0.8) | ||
.edgesIgnoringSafeArea(.all) | ||
|
||
if let imageInfo = images[safe: selectedImageIndex], | ||
let uiImage = loadImage(from: imageInfo.path) ?? loadImage(from: imageInfo.url) { | ||
Image(uiImage: uiImage) | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.scaleEffect(scale) | ||
.gesture( | ||
MagnificationGesture() | ||
.onChanged { value in | ||
self.scale = self.lastScale * value | ||
} | ||
.onEnded { _ in | ||
self.lastScale = self.scale | ||
} | ||
) | ||
.onTapGesture { | ||
isShown = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func loadImage(from imagePath: String?) -> UIImage? { | ||
guard let imagePath = imagePath else { return nil } | ||
let fileManager = FileManager.default | ||
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! | ||
let imageURL = documentsURL.appendingPathComponent(imagePath) | ||
return UIImage(contentsOfFile: imageURL.path) | ||
} | ||
} | ||
|
||
// extension to safely access array elements to avoid out-of-bounds | ||
extension Array { | ||
subscript(safe index: Int) -> Element? { | ||
return indices.contains(index) ? self[index] : nil | ||
} | ||
} |
Oops, something went wrong.