ios – Picture not saving to cameral roll utilizing Swift

[ad_1]

I’ve executed a good bit of analysis earlier than I got here right here, plenty of the ‘assist’ out there may be outdated.

My subject is I am making an attempt to save lots of a picture to the digital camera roll that shows in a viewer within the backside of the app as soon as the picture has been captured, I’ve written the perform that does the saving nonetheless when it says that it has saved, nothing has truly been saved into the Picture Library.

That is the code that saves the picture

import UIKit

class ImageSaver: NSObject {
    func writeToPhotoAlbum(picture: UIImage) {
        UIImageWriteToSavedPhotosAlbum(picture, self, #selector(saveCompleted), nil)
    }
    @objc func saveCompleted(_ picture: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    print("Save Completed!")
    }
}

That is the place the perform will get referred to as.

import UIKit
import SwiftUI

class CapturedImageView : UIView {
    //MARK:- Vars
    var picture : UIImage? {
        didSet {
            guard let picture = picture else {return}
            imageView.picture = picture
            let imageSaver = ImageSaver()
            imageSaver.writeToPhotoAlbum(picture: picture)

        }
    }

The remainder of the code from the file

import UIKit
import SwiftUI

class CapturedImageView : UIView {
    //MARK:- Vars
    var picture : UIImage? {
        didSet {
            guard let picture = picture else {return}
            imageView.picture = picture
            let imageSaver = ImageSaver()
            imageSaver.writeToPhotoAlbum(picture: picture)

        }
    }
    
    //MARK:- View Parts
    let imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 8
        imageView.clipsToBounds = true
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()
    
    //MARK:- Init
    override init(body: CGRect) {
        tremendous.init(body: .zero)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
    
    //MARK:- Setup
    func setupView(){
        translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = .white
        layer.cornerRadius = 10
        addSubview(imageView)
        
        NSLayoutConstraint.activate([
            imageView.topAnchor.constraint(equalTo: topAnchor, constant: 2),
            imageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2),
            imageView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2),
            imageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -2),
        ])
    }
}

[ad_2]

Leave a Reply