2012-02-13 11 views
0

最大1000pxの幅で画像のサイズを変更しようとしていますが、元の画像解像度の「比率」は変わらない必要があります。すなわち1000x1300または1000x1600など元の比率でUIImageをサイズ変更する

以下のコードにはどのような変更を加える必要がありますか?

- (void)setImageAndConvertToThumb:(UIImage *)image { 
//image 
UIImage *sizedImg = [image scaleWithMaxSize:CGSizeMake(1000, 1000) quality:kCGInterpolationHigh]; 
NSData *data = UIImagePNGRepresentation(sizedImg); 
self.image = data; 

} 
+1

可能重複[サイズのUIImageのサイズを変更する?](http://stackoverflow.com/questions/1703100/resize-uiimage-with-aspect-ratio) – DarkDust

答えて

0

だけ高さ(またはその逆)で割った幅は、これだけのように、原画像が持っていた同じ比率を使用して新しい高さを計算されたアスペクト比:

- (void)setImageAndConvertToThumb:(UIImage *)image { 
    //image 
    CGFloat newWidth = 1000; 
    CGFloat newHeight = newWidth * image.size.height/image.size.width; 
    UIImage *sizedImg = [image scaleWithMaxSize:CGSizeMake(newWidth, newHeight) quality:kCGInterpolationHigh]; 
    NSData *data = UIImagePNGRepresentation(sizedImg); 
    self.image = data; 
} 
+0

Brilliantありがとう:) – CraigBellamy

関連する問題