On Nov 2, 2020, at 5:59 PM, Carl Hoefs via Cocoa-dev 
<cocoa-dev@lists.apple.com> wrote:

> I have an iOS app that interacts with a macOS server process. The iOS app 
> takes a 3264x2448 camera image, scales it to 640x480 pixels, and makes a JPEG 
> representation of it to send to the server:

I have code that does pretty much the same thing, in Swift though so you’ll 
need to convert the API calls to ObjC. Since you’re taking a picture, you could 
use the AVCapturePhoto directly. 


    let capture : AVCapturePhoto
    private lazy var context = CIContext()

    lazy var remotePreviewImage: Data? = {
        guard let cgImage = 
self.capture.cgImageRepresentation()?.takeRetainedValue() else { return nil }

        var baseImg = CIImage(cgImage: cgImage)

        if let orientation = self.capture.metadata[ 
String(kCGImagePropertyOrientation) ] as? Int32 {
            baseImg = baseImg.oriented(forExifOrientation: orientation)
        }

        let scalePct = [800.0 / baseImg.extent.size.width, 800.0 / 
baseImg.extent.size.height].max() ?? 0.3
        let transformedImg = baseImg.transformed(by: CGAffineTransform(scaleX: 
scalePct, y: scalePct))
        print("generated remote preview image \(transformedImg.extent.size)")

        let colorspace : CGColorSpace = baseImg.colorSpace ?? 
CGColorSpace(name: CGColorSpace.sRGB)!
        let compressionKey = CIImageRepresentationOption(rawValue: 
kCGImageDestinationLossyCompressionQuality as String)
        let data = self.context.jpegRepresentation(of: transformedImg, 
colorSpace: colorspace,
                                                   options: [compressionKey : 
0.6])
        print("photo generated preview \(data?.count ?? 0) bytes")
        return data
    }()


I had a previous version that used ImageIO. I don’t remember why I switched but 
I still had the commented code hanging around.  

//    lazy var remotePreviewImage: Data? = {
//        guard let data = self.capture.fileDataRepresentation() else { return 
nil }
//        guard let src = CGImageSourceCreateWithData(data as NSData, nil) else 
{ return nil }
//        let thumbOpts = [
//            kCGImageSourceCreateThumbnailFromImageAlways: true,
//            kCGImageSourceCreateThumbnailWithTransform: true,
//            kCGImageSourceThumbnailMaxPixelSize: 800,
//        ] as [CFString : Any]
//
//        if let cgImage = CGImageSourceCreateThumbnailAtIndex(src, 0, 
thumbOpts as CFDictionary) {
//            // create jpg data
//            let data = NSMutableData()
//
//            if let dest = CGImageDestinationCreateWithData(data, kUTTypeJPEG, 
1, nil) {
//                CGImageDestinationAddImage(dest, cgImage, 
[kCGImageDestinationLossyCompressionQuality: 0.6] as CFDictionary)
//                CGImageDestinationFinalize(dest)
//            }
//            print("getPhoto generated preview \(data.count) bytes for 
RemoteCapture")
//            return data as Data
//        }
//        return nil
//    }()


Jim Crate

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to