美文网首页
ios 图片缩放

ios 图片缩放

作者: child_cool | 来源:发表于2017-03-07 22:32 被阅读0次
写在UIImage的分类中
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    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;
}

相关文章

  • 在 iOS 开发中如何优雅地进行图片缩放?

    在 iOS 开发中如何优雅地进行图片缩放? 在 iOS 开发中如何优雅地进行图片缩放?

  • iOS 图片缩放

    按指定尺寸缩放图片 根据屏幕的宽高等比压缩图片

  • iOS图片缩放

    概述 UIButton里要用到一张图片,但是实际的图片尺寸偏大,因此要根据真机的尺寸更改图片大小 方法 参考 io...

  • iOS图片缩放

    利用UIScrollView实现图片的缩放,适合新手。不多说了,直接上代码。

  • iOS 图片缩放

    需求分析 在App的图片查看中常见有以下功能: 图片随用户手指的点击,会进行相应区域的缩放,虽然这个简单的功能十分...

  • ios 图片缩放

  • iOS图片缩放

    1、直接使用UIKit提供的接口UIGraphicsImageRenderer 2、使用Core Graphics...

  • flutter开发常见问题

    1.android获取图片上的二维码会对图片进行缩放处理,可能导致获取失败。要控制缩放比例 2.iOS14以上,使...

  • iOS scrollView缩放图片

    2019.2.27更新: 简化了代码,逻辑更加清晰 个人认为本文正确,优雅地实现了缩放图片。总体效果: 源码地址:...

  • cocos2d-x 优化

    一,IOS与图片内存在IOS上,图片会被自动缩放到2的N次方大小。比如一张10241025的图片,占用的内存与一张...

网友评论

      本文标题:ios 图片缩放

      本文链接:https://www.haomeiwen.com/subject/lhyvgttx.html