2013-03-15 3 views

답변

13

이것은 과잉 일 수 있지만 이미지를 가져 와서 원하는 해상도로 그래픽 컨텍스트를 만든 다음 tempImage를 UIImageView로 설정하고 덮어 쓰면됩니다.

 UIImage *image = YourImageView.image; 
     UIImage *tempImage = nil; 
     CGSize targetSize = CGSizeMake(80,60); 
     UIGraphicsBeginImageContext(targetSize); 

     CGRect thumbnailRect = CGRectMake(0, 0, 0, 0); 
     thumbnailRect.origin = CGPointMake(0.0,0.0); 
     thumbnailRect.size.width = targetSize.width; 
     thumbnailRect.size.height = targetSize.height; 

     [image drawInRect:thumbnailRect]; 

     tempImage = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     YourImageView.image = tempImage; 
+0

왜 과다한 ??? 내 말은, 그것이 효과가 있지만 부정적인 영향을 미치나요? – user1602687

+0

몇 줄을 원했다. 이것은 10, 롤과 같습니다. 아니 부정적인 영향을! – ApolloSoftware

+0

나는 이걸 시도했지만 흐릿 해지는거야? – Lion789

1

사용이 클래스 (따라 파일을있는 .h하는 코드를 추가)

#import "UIImage+Resize.h" 

@implementation UIImage (Resize) 

- (UIImage *)resizedImage:(CGSize)bounds { 
    return [self resizedImage:bounds upScale:YES]; 
} 

- (UIImage*)resizedImage:(CGSize)bounds upScale:(BOOL)upScale { 
    CGSize originalSize = self.size; 
    float xScale = bounds.width/originalSize.width; 
    float yScale = bounds.height/originalSize.height; 
    float scale = MIN(xScale, yScale); 

    if (!upScale) { 
     scale = MIN(scale, 1); 
    } 

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale); 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale); 
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return resultImage; 
} 
1
- (void)resizeImage:(UIImage *)image 
{ 
    CGSize origImageSize = [image size]; 
    CGRect imageRect = CGRectMake(0, 0, 80, 60); 
    float ratio = MAX(newRect.size.width/origImageSize.width, 
        newRect.size.height/origImageSize.height); 
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0); 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect 
               cornerRadius:5.0]; 
    [path addClip]; 
    CGRect imageRect; 
    imageRect.size.width = ratio * origImageSize.width; 
    imageRect.size.height = ratio * origImageSize.height; 
    imageRect.origin.x = (newRect.size.width - imageRect.size.width)/2.0; 
    imageRect.origin.y = (newRect.size.height - imageRect.size.height)/2.0; 
    [image drawInRect:imageRect]; 
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *data = UIImagePNGRepresentation(smallImage); 
    UIGraphicsEndImageContext(); 
} 

가있는 CoreGraphics의 frameawork를 추가하는 것을 잊지 마십시오.

0

나는 this page에 다음 코드를 발견했습니다

- (UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width{ 

if (sourceImage.size.width>sourceImage.size.height) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

float oldWidth = sourceImage.size.width; 
float scaleFactor = i_width/oldWidth; 
float newHeight = sourceImage.size.height * scaleFactor; 
float newWidth = oldWidth * scaleFactor; 

UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
return newImage; 
} 

당신이해야 할 i_width로 제공이며, 그것은 적절하게 조정됩니다.

그림이 가로 모드 인 경우 그림이 세로로 회전 된 다음 크기가 조정되도록 약간 변형되었습니다. 당신이 반대 (가로 세로)로하려면이 변경이에

if (sourceImage.size.width>sourceImage.size.height) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

:

if (sourceImage.size.height>sourceImage.size.width) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

주의 : 사진이 가리키는 경우 내 회전 방법은 고려하지 않는다 왼쪽 아니면 오른쪽. 즉, 이미지가 가로로 뒤집혀 있으면 내 코드에서이를 인식 할 수 없습니다. 나는 다른 누군가가 이것에 대한 약간의 빛을 비춰 줄 수 있기를 바랍니다 :)

관련 문제