2014-11-13 1 views
0

imageView은 콘텐츠 모드에서 표시되는 크기의 이미지를 가져올 수 있으며 네이티브 속성에 따라 달라지지는 않습니다.그에 따라 UIImageView에서 UIImage 가져 오기

코드 :

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WID, WID)]; 
imageView.center = CGPointMake(point.x, point.y + Y_OFFSET); 
imageView.image = [UIImage imageNamed:@"img"]; 
imageView.contentMode = UIViewContentModeScaleAspectFit; 
+0

이미지를 얻으려면하거나 이미지의 경계 (CGrect를) 싶은가? – emotality

답변

0

그런 다음 다시 이미지를 그릴에 저장해야합니다.

// Image frame size 
CGSize size = imageView.bounds.size; 
// Grab a new CGContext 
UIGraphicsBeginImageContextWithOptions(size, false, 0.0); 
// Draw the image 
[image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
// Grab the new image 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

위의 코드는 프레임에 이미지를 그려 테두리로 뻗어 있습니다. 어떻게 그려 지는지 다른 모드를 원한다면 직접 계산하고 "이미지 그릴"코드 줄에 원하는 것을 넣어야합니다.

- (CGRect) aspectFittedRect:(CGSize)inSize max:(CGRect)maxRect { 
    float originalAspectRatio = inSize.width/inSize.height; 
    float maxAspectRatio = maxRect.size.width/maxRect.size.height; 

    CGRect newRect = maxRect; 
    if (originalAspectRatio > maxAspectRatio) { // scale by width 
     newRect.size.height = maxRect.size.height * inSize.height/inSize.width; 
     newRect.origin.y += (maxRect.size.height - newRect.size.height)/2.0; 
    } else { 
     newRect.size.width = maxRect.size.height * inSize.width/inSize.height; 
     newRect.origin.x += (maxRect.size.width - newRect.size.width)/2.0; 
    } 

    return CGRectIntegral(newRect); 
} 

그냥 inSize과 같은 maxRect imageView.bounds로 전달 imageView.image.size :

예를 들어, 애스펙트 착용감이 알고리즘을 확인.

출처 : http://iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/15001-aspect-fit-algorithm.html

+0

이미지의 가로 세로 비율이 적절하게 조정되었지만 크기가'imageView'의 크기보다 상당히 작습니다. 무엇이 이것을 일으킬 수 있습니까? –

+0

상당히 작 으면 길이와 높이가 절반입니까? –

+0

내가 볼 :) 완벽하게 지금 작동합니다. 고맙습니다! –

관련 문제