• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Scaling a UIImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scaling a UIImage


  • Subject: Re: Scaling a UIImage
  • From: Carl Hoefs via Cocoa-dev <email@hidden>
  • Date: Wed, 4 Nov 2020 15:10:31 -0800

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

-Carl

> On Nov 4, 2020, at 2:17 PM, Alex Zavatone <email@hidden> 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
>> <email@hidden <mailto:email@hidden>> wrote:
>>
>> On Nov 2, 2020, at 5:59 PM, Carl Hoefs via Cocoa-dev
>> <email@hidden <mailto:email@hidden>> 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 (email@hidden
>> <mailto:email@hidden>)
>>
>> 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:
>>
>> This email sent to email@hidden
>

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Scaling a UIImage
      • From: Alex Zavatone via Cocoa-dev <email@hidden>
References: 
 >Scaling a UIImage (From: Carl Hoefs via Cocoa-dev <email@hidden>)
 >Re: Scaling a UIImage (From: James Crate via Cocoa-dev <email@hidden>)
 >Re: Scaling a UIImage (From: Alex Zavatone via Cocoa-dev <email@hidden>)

  • Prev by Date: Re: Scaling a UIImage
  • Next by Date: Re: Scaling a UIImage
  • Previous by thread: Re: Scaling a UIImage
  • Next by thread: Re: Scaling a UIImage
  • Index(es):
    • Date
    • Thread