UIImage大小和裁剪适合框架

时间:2020-12-20 08:57:45

I know this question has been asked several times, but their answers make my images loose quality. They all become pixelated. So even though it crops and resizes correctly it looses quality.

我知道这个问题已经被问过好几次了,但是他们的回答让我的图像质量很差。他们都成为像素化。因此,即使它种植和大小正确,它失去了质量。

Just so you can check it, this is the algorithm which is in every post:

你可以检查一下,这是每个帖子里的算法:

- (UIImage*)scaleAndCropImage:(UIImage *)aImage forSize:(CGSize)targetSize
{
UIImage *sourceImage = aImage;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;

    if (widthFactor > heightFactor)
    {
        scaleFactor = widthFactor; // scale to fit height
    }
    else
    {
        scaleFactor = heightFactor; // scale to fit width
    }

    scaledWidth  = width * scaleFactor;
    scaledHeight = height * scaleFactor;

    // center the image
    if (widthFactor > heightFactor)
    {
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
    }
    else
    {
        if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
}

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil)
{
    NSLog(@"could not scale image");
}

//pop the context to get back to the default
UIGraphicsEndImageContext();

return newImage;
 }

2 个解决方案

#1


0  

You should use UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0) instead of UIGraphicsBeginImageContext(targetSize) so the correct scale factor gets applied to the bitmap.

您应该使用UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0)而不是UIGraphicsBeginImageContext(targetSize),以便将正确的比例因子应用到位图。

Specifying 0.0 as scale factor, sets the scale factor to that from the device's main screen. Calling only UIGraphicsBeginImageContext() is the same as calling UIGraphicsBeginImageContextWithOptions(..) with a scale factor of 1.0

指定0.0作为比例因子,从设备的主屏幕设置比例因子。只调用UIGraphicsBeginImageContext()与调用UIGraphicsBeginImageContextWithOptions(.)的比例因子为1.0是一样的

for more details take a look at: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIGraphicsBeginImageContextWithOptions

更多细节请看:http://developer.apple.com/library/ios/documentation/uikit/reference/reference/reference/reference.html #/ apple_ref/c/func/ uigraphicbeginimagecontextwithoptions

#2


5  

On the line where your begin the UIGraphicsImageContext, use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext. Try something like this:

在开始UIGraphicsImageContext的那一行,使用UIGraphicsBeginImageContextWithOptions而不是UIGraphicsBeginImageContext。试试这样:

UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0)

Notice the three parameters passed above, I'll go through them in order:

注意上面传递的三个参数,我将依次进行:

  1. targetSize is the size of the image measured in points (not pixels)
  2. targetSize是用点(不是像素)测量的图像的大小
  3. NO is a BOOL value (could be YES or NO) that indicates whether the image is 100% opaque or not. Setting this to NO will preserve transparency and create an alpha channel to handle transparency.
  4. NO是BOOL值(可以是或否),它指示图像是否为100%不透明。设置为NO将保持透明度,并创建一个alpha通道来处理透明度。
  5. The most important part of the above code is the final parameter, 0.0. This is the image scale factor that will be applied. Specifying the value to 0.0 sets the scale factor of the current device's screen. This means that the quality will be preserved, and look especially good on Retina Displays.
  6. 上述代码中最重要的部分是最终参数0.0。这是将要应用的图像比例因子。将值指定为0.0将设置当前设备屏幕的比例因子。这意味着质量将被保留,并且在视网膜显示器上看起来特别好。

Here's the Apple Documentation on UIGraphicsBeginImageContextWithOptions.

这是UIGraphicsBeginImageContextWithOptions的苹果文档。

#1


0  

You should use UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0) instead of UIGraphicsBeginImageContext(targetSize) so the correct scale factor gets applied to the bitmap.

您应该使用UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0)而不是UIGraphicsBeginImageContext(targetSize),以便将正确的比例因子应用到位图。

Specifying 0.0 as scale factor, sets the scale factor to that from the device's main screen. Calling only UIGraphicsBeginImageContext() is the same as calling UIGraphicsBeginImageContextWithOptions(..) with a scale factor of 1.0

指定0.0作为比例因子,从设备的主屏幕设置比例因子。只调用UIGraphicsBeginImageContext()与调用UIGraphicsBeginImageContextWithOptions(.)的比例因子为1.0是一样的

for more details take a look at: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIGraphicsBeginImageContextWithOptions

更多细节请看:http://developer.apple.com/library/ios/documentation/uikit/reference/reference/reference/reference.html #/ apple_ref/c/func/ uigraphicbeginimagecontextwithoptions

#2


5  

On the line where your begin the UIGraphicsImageContext, use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext. Try something like this:

在开始UIGraphicsImageContext的那一行,使用UIGraphicsBeginImageContextWithOptions而不是UIGraphicsBeginImageContext。试试这样:

UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0)

Notice the three parameters passed above, I'll go through them in order:

注意上面传递的三个参数,我将依次进行:

  1. targetSize is the size of the image measured in points (not pixels)
  2. targetSize是用点(不是像素)测量的图像的大小
  3. NO is a BOOL value (could be YES or NO) that indicates whether the image is 100% opaque or not. Setting this to NO will preserve transparency and create an alpha channel to handle transparency.
  4. NO是BOOL值(可以是或否),它指示图像是否为100%不透明。设置为NO将保持透明度,并创建一个alpha通道来处理透明度。
  5. The most important part of the above code is the final parameter, 0.0. This is the image scale factor that will be applied. Specifying the value to 0.0 sets the scale factor of the current device's screen. This means that the quality will be preserved, and look especially good on Retina Displays.
  6. 上述代码中最重要的部分是最终参数0.0。这是将要应用的图像比例因子。将值指定为0.0将设置当前设备屏幕的比例因子。这意味着质量将被保留,并且在视网膜显示器上看起来特别好。

Here's the Apple Documentation on UIGraphicsBeginImageContextWithOptions.

这是UIGraphicsBeginImageContextWithOptions的苹果文档。