2013-03-05 2 views
0

두 개의 UIImageView가 있는데 하나에서 다른 이미지로 복사하고 색상을 조정하고 싶지만 이상한 스트레칭이 발생합니다. 이 같은 내가 첫 번째 라인을 복사 이미지 벌금을 사용하는 경우UIImage 색상으로 그리기

_selectedIcon.image = groupView.icon; 
[ViewUtils colorImageView:_selectedIcon withRed:0.843 Green:0.3176 andBlue:0.066667]; 

과 같습니다 :

Correct

그것은이다 여기에

내가 복사본을 만드는 데 사용하는 코드입니다 강조 표시된 모양이기 때문에 조금 더 큽니다. 그러나 그것을 복사하고 올바르게 크기를 조정합니다.

+(void)colorImageView:(UIImageView*)source withRed:(CGFloat)red Green:(float)green andBlue:(float)blue 
{ 
    // Create a context containing the image. 
    UIGraphicsBeginImageContext(source.bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // The mask needs to be upside down for some reason 
    CGContextTranslateCTM(context, 0, source.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextClipToMask(context, source.bounds, [source.image CGImage]); 
    CGContextFillRect(context, source.bounds); 

    [[UIColor colorWithRed:red green:green blue:blue alpha:1.0] set]; 
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:source.bounds]; 
    [imagePath fill]; 
    // Retrieve the new image. 
    source.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 

나는 이미지 스케일링의 시도 여러 가지 방법이있다 : 여기

Wrong

내가 이미지 색상을 사용하는 코드입니다 : 나는 그것을 색상하려고 할 때 다음에,이 수행 그리고 아무것도 작동하지 않는 것 같습니다. 누구 제안이 있습니까?

답변

0

알아 냈습니다. 이미지는 UIImageView 전체에 맞게 늘어나므로 테두리를 조정하여 올바르게 크기를 조정해야했습니다. 다음은 내가 사용한 코드입니다.

float ratioX = source.bounds.size.width/source.image.size.width; 
float ratioY = source.bounds.size.height/source.image.size.height; 
float ratio = MIN(ratioX, ratioY); 
CGRect bounds = CGRectMake(0, 0, source.image.size.width * ratio, source.image.size.height * ratio); 
관련 문제