2013-07-31 116 views
0

如何在不使用CGAffineTransform的情况下绘制比上下文大小更小的图像?renderInContext指定尺寸的图像绘制

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

的问题是,如果imageView.image.sizesize它将被绘制为中心,但在边界(而不是绘制区域大的图像)切更大。我想要图像适合,所以想减小尺寸imageView.image.size。有没有什么办法可以使用renderInContext来实现?

回答

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; 
} 
+0

为downvote任何意见或只是曳身边?如果我做错了什么,会很好地告知它是什么。 – Vive

+0

我正在寻找一些方法来渲染图层到不同的大小,最后我找到了方法,您的答案可能会误导其他人类似于我。 – lbsweek

0

为什么你想这样做而不改变上下文?这是完成这项工作的恰当工具!

这里最简单的方法是拨打CGContextScaleCTM将比例因子应用于上下文。该比例因子将影响到所有上下文,包括致电-renderIntoContext:

+0

因为上下文已经被转换,我需要它保持原样 - 使用'saveCGState'n'Restore'也会产生问题。无论如何,我已经找到解决我的问题,并在这里描述,明天会接受答案(不能早)。 – Vive

+0

如果'CGContextSaveGState' /'CGContextRestoreGState'没有恢复上下文的CTM,那么你正在做其他的错误。你的方法至少使用了两倍的内存,并且至少需要两倍的时间来执行上下文的转换。就像我说的那样:改变背景是工作的“准确”*正确工具。 – ipmcc

-1

这是缩放大小的版本,只需将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; 
} 
+0

CGContextSaveGState非常消耗内存。我没有看到这个代码比我的优势,你能提供任何评论? – Vive