Thanks for the UIImage category resizing methods! They are quite useful.

-Carl

> On Nov 4, 2020, at 2:17 PM, Alex Zavatone <z...@mac.com> wrote:
> 
> Sorry for the delay.  I hope these do what you need.
> Of course you’ll need to add checks to make sure that you’re not dividing by 
> zero or nil.
> 
> 
> 
> // Alex Zavatone 4/2/16.
> + (UIImage *)imageWithImage:(UIImage *)image 
> scaledToHeight:(CGFloat)newHeight 
> {    
>     CGFloat ratio = newHeight / image.size.height;
>     CGFloat newWidth = image.size.width * ratio;
>     
>     CGSize newSize = CGSizeMake(newWidth, newHeight);
>     //UIGraphicsBeginImageContext(newSize);
>     // In next line, pass 0.0 to use the current device's pixel scaling 
> factor (and thus account for Retina resolution).
>     // Pass 1.0 to force exact pixel size.
>     UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
>     [image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
>     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
>     UIGraphicsEndImageContext();
>     return newImage;
> }
> 
> 
> + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize 
> {    
>     //UIGraphicsBeginImageContext(newSize);
>     // In next line, pass 0.0 to use the current device's pixel scaling 
> factor (and thus account for Retina resolution).
>     // Pass 1.0 to force exact pixel size.
>     UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
>     [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
>     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
>     UIGraphicsEndImageContext();
>     return newImage;
> }
> 
> + (UIImage *)imageWithImage:(UIImage *)image 
> scaledToPercentage:(CGFloat)newScale 
> {
>     CGSize newSize = CGSizeMake(image.size.width * newScale, image.size.width 
> * newScale);
>     UIImage *newImage = [self imageWithImage:image scaledToSize:newSize];
>     
>     return newImage;
> }
> 
> 
> Cheers,
> Alex Zavatone
> 
> 
>> On Nov 3, 2020, at 10:34 AM, James Crate via Cocoa-dev 
>> <cocoa-dev@lists.apple.com <mailto:cocoa-dev@lists.apple.com>> wrote:
>> 
>> On Nov 2, 2020, at 5:59 PM, Carl Hoefs via Cocoa-dev 
>> <cocoa-dev@lists.apple.com <mailto: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 
>> <mailto: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 
>> <http://lists.apple.com/>
>> 
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com 
>> <https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com>
>> 
>> This email sent to z...@mac.com
> 

_______________________________________________

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