Picture Class with person scroll enhancing
I am making a Picture Class Perform addImage that shrinks the picture to the body width dimension, the crops it, and reserve it 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 person, much like bigger social media apps. I am going to replace that a part of the code on this class and put up it right here once I get that far.
Present Problem
The difficulty i am dealing with now’s 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 information: Information?
}
class Picture {
var title: String = UUID().uuidString
var picture: UIImage?
var information: 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(information: self.information!)!
}
func addImage() {
do {
print(picture?.cgImage?.bytesPerRow)
let uiimg = UIImage(useful resource: ImageResource(title: "image-placeholder.jpg", bundle: .primary))
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), top: Int(cgIMG.top - 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.youngster(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 information = attempt JSONSerialization.information(withJSONObject: json)
let url = URL(string: "https://us-central1-realtime-missions.cloudfunctions.internet/location-check")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = information
request.addValue("utility/json", forHTTPHeaderField: "Content material-Kind")
request.addValue("/", forHTTPHeaderField: "Settle for")
let process = URLSession.shared.dataTask(with: request) { information, response, error in
let statusCode = (response as! HTTPURLResponse).statusCode
if statusCode == 200 {
print("SUCCESS")
do {
if let information = information {
let decoder = JSONDecoder()
let imageData = attempt JSONDecoder().decode(ImageData.self, from: information)
self.title = imageData.title!
self.information = imageData.information!
self.reintalizeImage()
}
} catch {
print(error)
}
} else {
print("FAILURE")
}
}
process.resume()
} catch {
print(error)
}
}
}
Edited:
- I eliminated loads 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 based mostly on the picture dimension discount ratio. That could be the place the difficulty is.