macOS中UIGraphicsBeginImageContextWithOptions的替代实现及图片缩放方案
macOS Image Scaling Equivalent to iOS's
UIGraphicsBeginImageContextWithOptions Got it, let's port that iOS image scaling logic to macOS smoothly. The main differences are that macOS uses NSImage instead of UIImage, and the graphics context setup works with NSBitmapImageRep rather than UIKit's graphics functions. Here's a drop-in replacement that matches your original iOS behavior perfectly:
Scaling Implementation Code
+ (NSImage *)image:(NSImage *)originalImage scaledToSize:(NSSize)desiredSize { // Match iOS's screen scale behavior with macOS's backing scale factor CGFloat scaleFactor = [NSScreen mainScreen].backingScaleFactor; NSSize pixelDimensions = NSMakeSize(desiredSize.width * scaleFactor, desiredSize.height * scaleFactor); // Create a bitmap representation with the correct pixel size and scale NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:pixelDimensions.width pixelsHigh:pixelDimensions.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:32]; // Set the logical size (in points) to match the desired output bitmapRep.size = desiredSize; // Set up the graphics context and draw the original image NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmapRep]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:context]; // Draw the image stretched to the desired size (matches iOS's drawInRect behavior) [originalImage drawInRect:NSMakeRect(0, 0, desiredSize.width, desiredSize.height) fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0]; // Clean up and assemble the final image [NSGraphicsContext restoreGraphicsState]; NSImage *finalImage = [[NSImage alloc] initWithSize:desiredSize]; [finalImage addRepresentation:bitmapRep]; return finalImage; }
Key Details Explained
- Scale Factor Handling: We use
[NSScreen mainScreen].backingScaleFactorto replicate iOS's[UIScreen mainScreen].scale, ensuring the scaled image looks sharp on high-DPI Retina displays. - Bitmap Setup: The
NSBitmapImageRepis configured with 8 bits per sample, 4 samples per pixel (RGBA), and a calibrated RGB color space—this matches the default context setup from iOS'sUIGraphicsBeginImageContextWithOptions. - Drawing Behavior: The
drawInRect:call stretches the original image to fill the desired size, just like your iOS code does. If you wanted to maintain aspect ratio instead, you'd calculate a fitting rect first, but this matches your original requirement exactly.
Saving the Scaled Image
If you need to save the scaled image to a file (like your iOS code's "save scaled copy" intent), you can convert the NSImage to data using the bitmap rep:
NSImage *scaledImage = [YourClass image:originalImage scaledToSize:NSMakeSize(100, 100)]; NSBitmapImageRep *imageRep = [scaledImage representations][0]; NSData *pngData = [imageRep representationUsingType:NSPNGFileType properties:@{}]; [pngData writeToFile:@"/path/to/scaled_image.png" atomically:YES];
内容的提问来源于stack exchange,提问作者Alexandr Chekel
相关产品推荐
相关产品推荐

