0

如何在不使用 CGAffineTransform 的情况下绘制小于上下文大小的图像?

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// center image
CGContextTranslateCTM(context, placement.origin.x, placement.origin.y);
[imageView.layer renderInContext:context];

问题是如果imageView.image.size大于size它将被绘制在中心,但在边界处切割(比绘图区域更大的图像)。我希望图像适合,所以想减小imageView.image.size. 有什么方法可以实现这一点renderInContext吗?

4

3 回答 3

0

通过[imageView.layer renderInContext:context];您无法更改大小(或者我没有找到方法)。这样做的唯一选择是现在使用 drawImage 创建图像(根据请求的大小),分配新的 UIImageView 并绘制其图层。

UIImage *resizedImage = [self createNewImageFrom:currentImage withSize:size];
UIImageView *imageView = [[UIImageView alloc] initWithImage:resizedImage];
// ...
[imageView.layer renderInContext:context];


- (UIImage*)createNewImageFrom:(UIImage*)image withSize:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0f, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}
于 2013-07-31T12:05:44.177 回答
0

为什么要在不改变上下文的情况下这样做?这是完成这项工作的最佳工具!

这里最简单的方法是调用CGContextScaleCTM以将比例因子应用于上下文。该比例因子将影响到上下文的所有绘图,包括对-renderIntoContext:.

于 2013-07-31T13:21:04.200 回答
-1

这是缩放尺寸的版本,只需将 scale 替换为 size.width 和 size.height 即可获得任何目标尺寸。

对于 UIView:

-(UIImage*)convertViewToImage:(UIView*)v withScale:(float)scale{
    //create bitmap context with scaled size
    CGSize s = v.bounds.size;
    s.width *= scale;
    s.height *= scale;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);

    //scale CTM to render and resotre CTM
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextScaleCTM(context, scale, scale);
    [v.layer renderInContext: context];
    CGContextRestoreGState(context);

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

对于 UIScrollView

-(UIImage*)convertScrollContentToImage:(UIScrollView*)sv withScale:(float)scale{
    //create bitmap context with scaled size
    CGSize s = sv.contentSize;
    s.width *= scale;
    s.height *= scale;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);


    {
        //set scroll.frame.size to contentSize, and contentOffset to 0
        CGPoint savedContentOffset = sv.contentOffset;
        CGRect savedFrame = sv.frame;

        sv.contentOffset = CGPointZero;
        sv.frame = CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height);

        //scale CTM to render and resotre CTM
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextScaleCTM(context, scale, scale);
        [sv.layer renderInContext: context];
        CGContextRestoreGState(context);

        //set scroll.frame.size and ContentOffset back
        sv.contentOffset = savedContentOffset;
        sv.frame = savedFrame;

    }

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
于 2014-10-26T15:44:29.683 回答