Picture Class with consumer scroll modifying
I am making a Picture Class Perform addImage that shrinks the picture to the body width measurement, the crops it, and put it aside to firebase.
I plan on including the power to avoid wasting a uncropped picture additionally, so the crop origin level will be edited later by the consumer, much like bigger social media apps. I will replace that a part of the code on this class and publish it right here after I get that far.
Present Challenge
The difficulty i am dealing with now could be within the creation of a CGImage from the UIImage.CGImage.
The CGImage is returning nil when i am creating it.
class ImageData: Decodable {
var title: String?
var knowledge: Information?
}
class Picture {
var title: String = UUID().uuidString
var picture: UIImage?
var knowledge: Information?
var body: CGRect?
init() {
self.picture = UIImage(named: self.title)
}
func addData(picture: UIImage, body: CGRect) {
self.picture = picture
self.body = body
}
func reintalizeImage() {
self.picture = UIImage(knowledge: self.knowledge!)!
}
func addImage() {
do {
print(picture?.cgImage?.bytesPerRow)
let uiimg = UIImage(useful resource: ImageResource(title: "image-placeholder.jpg", bundle: .most important))
let bitMapInfo: CGBitmapInfo = CGBitmapInfo(rawValue: (picture?.cgImage!.bitmapInfo.rawValue)!)
let cgIMG = (picture?.cgImage)! as CGImage
let intent = CGColorRenderingIntent(rawValue: cgIMG.renderingIntent.rawValue)
let ImgFrame = body! as CGRect
let widthDifference = Int(CGFloat(cgIMG.width) - ImgFrame.width)
let colorSpace: CGColorSpace = CGColorSpace(title: CGColorSpace.sRGB)!;
let newCGImage: CGImage = CGImage(width: Int(ImgFrame.width), peak: Int(cgIMG.peak - widthDifference), bitsPerComponent: cgIMG.bitsPerComponent, bitsPerPixel: 72, bytesPerRow: cgIMG.bytesPerRow, house: colorSpace, bitmapInfo: bitMapInfo, supplier: cgIMG.dataProvider!, decode: cgIMG.decode, shouldInterpolate: false, intent: intent!)!
let newCroppedCGImage = newCGImage.cropping(to: body!)!
let croppedImage = UIImage(cgImage: newCroppedCGImage)
let storage = Storage.storage(url:"gs://realtime-missions-images")
let storageRef = storage.reference()
let fileRef = storageRef.little one(title + ".txt")
// metadata --> uid, imageID,
let uploadTask = fileRef.putData(croppedImage.pngData()!, metadata: nil) { (metadata, error) in
if let error = error {
print(error)
}
}
} catch {
print(error)
}
}
func loadImage(imageID: String) {
do {
var json = ["requestType":"loadImage", "imageID": imageID] as [String : Any]
let knowledge = attempt JSONSerialization.knowledge(withJSONObject: json)
let url = URL(string: "https://us-central1-realtime-missions.cloudfunctions.web/location-check")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = knowledge
request.addValue("software/json", forHTTPHeaderField: "Content material-Kind")
request.addValue("/", forHTTPHeaderField: "Settle for")
let job = URLSession.shared.dataTask(with: request) { knowledge, response, error in
let statusCode = (response as! HTTPURLResponse).statusCode
if statusCode == 200 {
print("SUCCESS")
do {
if let knowledge = knowledge {
let decoder = JSONDecoder()
let imageData = attempt JSONDecoder().decode(ImageData.self, from: knowledge)
self.title = imageData.title!
self.knowledge = imageData.knowledge!
self.reintalizeImage()
}
} catch {
print(error)
}
} else {
print("FAILURE")
}
}
job.resume()
} catch {
print(error)
}
}
}
Edited:
- I eliminated a variety of the optionals. However i am nonetheless getting an error on
creating a brand new CGImage with the init. It return nil.
At present Working On:
I’ll attempt to scale back the bytes per row worth primarily based on the picture measurement discount ratio. That could be the place the difficulty is.