iOS Development

ios – Situation Saving Picture to Digicam Roll in Swift

Spread the love


I’m experimenting with utilizing ContentMenu to permit customers to do issues corresponding to saving pictures to their digicam roll. Presently within the code, the worth of the displayed picture is of sort Picture so I made a operate that is purported to convert it into a price of sort UIImage after which the picture can be saved to the digicam roll through UIImageWriteToSavedPhotosAlbum(). The code works, nevertheless after I go to view the saved picture within the digicam roll it seems as an all-black picture, moderately than the precise picture itself.

The code for the conversion is as follows:

extension Picture {
    func asUIImage() -> UIImage? {
        // Create a UIView
        let uiView = UIView(body: CGRect(x: 0, y: 0, width: 100, top: 100))
        
        // Create a internet hosting controller with the SwiftUI Picture
        let hostingController = UIHostingController(rootView: self)
        
        // Assign the body to the internet hosting controller
        hostingController.view.body = uiView.bounds
        
        // Add the internet hosting controller's view to the UIView
        uiView.addSubview(hostingController.view)
        
        // Make the snapshot of the internet hosting controller's view
        UIGraphicsBeginImageContextWithOptions(uiView.bounds.measurement, uiView.isOpaque, 0.0)
        defer { UIGraphicsEndImageContext() }
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        uiView.layer.render(in: context)
        let uiImage = UIGraphicsGetImageFromCurrentImageContext()
        
        return uiImage
    }
}

which known as right here:

func manyMediaView(media: [MediaOBJ]) -> some View {
        GeometryReader { geo in
            TabView {
                ForEach(media.indices, id: .self) { index in
                    AsyncImage(url: media[index].hyperlink) { part in
                        swap part {
                        case .success(let picture):
                            picture
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    selectedImageIndex = index
                                    showFullImage = true
                                }
                                .contextMenu {
                                    Button(motion: {
                                        // Motion to repeat the photograph
                                    }) {
                                        Textual content("Copy Photograph")
                                        Picture(systemName: "doc.on.doc")
                                    }
                                    
                                    Button(motion: {
                                        // Motion to avoid wasting the photograph
                                        if let uiImage = picture.asUIImage() {
                                            UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil)
                                            print("Saved efficiently!")
                                        } else {
                                            print("Error: Unable to transform Picture to UIImage.")
                                        }
                                    }) {
                                        Textual content("Save Photograph")
                                        Picture(systemName: "sq..and.arrow.down")
                                    }
                                    
                                    ShareLink(merchandise: viewModel.put up.url) {
                                        Textual content("Share through ...")
                                        Picture(systemName: "sq..and.arrow.up")
                                    }
                                }
                        case .failure:
                            DefaultPlaceholder()
                                .maxHeight(600)
                                .onAppear {
                                    imageLoadKey = .init()
                                }
                        case .empty:
                            Coloration.clear
                        }
                    }
                    .tag(index)
                    .body(maxWidth: .infinity, maxHeight: .infinity)
                }
            }
            .cornerRadius(16)
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .all the time))
            .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .all the time))
            .gesture(
                DragGesture().onChanged { worth in
                    if worth.translation.width > 50 {
                        selectedImageIndex = max(selectedImageIndex - 1, 0)
                    } else if worth.translation.width < -50 {
                        selectedImageIndex = min(selectedImageIndex + 1, media.rely - 1)
                    }
                }
            )
            .fullScreenCover(isPresented: $showFullImage) {
                MultiImageGalleryView(imageUrls: media.map { $0.hyperlink }, isPresented: $showFullImage, selectedImageIndex: $selectedImageIndex)
            }
            .onTapGesture {
                // Deliberately empty to forestall auto navigation to SinglePostView
            }
        }
    }

What am I lacking that’ll let the photographs be correctly saved?

Leave a Reply

Your email address will not be published. Required fields are marked *