ios 调整图像大小 Objective-C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10334581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:06:16  来源:igfitidea点击:

Resizing Images Objective-C

iosobjective-cuiimageuikit

提问by chrisw

I'm trying to downscale an image in Objective-C taken with the iPhone 4 and above so I can send it to the server as quickly as possible. At the moment the image at full size is taking ages.

我正在尝试缩小使用 iPhone 4 及更高版本拍摄的 Objective-C 图像,以便我可以尽快将其发送到服务器。目前,全尺寸图像需要很长时间。

The current of downsizing I am trying is:

我正在尝试的缩小规模的当前是:

CGSize imageSize;

imageSize = CGSizeMake(484, 888);

UIImage *img = [UIImage imageWithContentsOfFile:path];

UIGraphicsBeginImageContext( imageSize );
[img drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
UIImage* realfrontImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *frontImageData = UIImageJPEGRepresentation(realfrontImage, 1.0);

This gives me an image of 320x460 how do I get it the size I wanted 484x888.

这给了我一个 320x460 的图像,我如何得到我想要的 484x888 的大小。

回答by Anshuk Garg

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

use this method to scale down your images.

使用此方法缩小图像。

回答by Nitin

// call this function with your resize ratio...

// 使用您的调整大小比率调用此函数...

 -(UIImage*)scaleByRatio:(float) scaleRatio
{ 
  CGSize scaledSize = CGSizeMake(self.size.width * scaleRatio, self.size.height * scaleRatio);
   CGColorSpaceRef colorSpace;
   int   bitmapBytesPerRow;
   bitmapBytesPerRow   = (size.width * 4);

    //The output context.   
   UIGraphicsBeginImageContext(scaledSize);
   CGContextRef context = context = CGBitmapContextCreate (NULL,
                             scaledSize .width,
                             scaledSize .height,
                             8,      // bits per component
                             bitmapBytesPerRow,
                             colorSpace,
                             kCGImageAlphaPremultipliedLast);

 //Percent (101%)    
 #define SCALE_OVER_A_BIT 1.01

//Scale.
CGContextScaleCTM(context, scaleRatio * SCALE_OVER_A_BIT, scaleRatio *      SCALE_OVER_A_BIT);
[self drawAtPoint:CGPointZero];

  //End?
  UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return scaledImage;
}

Hope, this will help you...

希望能帮到你...

回答by Paul Peelen

Maybe this post helps you out: How to resize the image programmatically in objective-c in iphone

也许这篇文章可以帮到你:How to resize the image in Objective-c in iphone

Beware... you are doing this in Objective-C, ... not XCode. XCode is the application you develop Objective-c within. Just like Netbeans or Eclipse is commonly used for Java and PHP.

当心......你是在Objective-C中这样做的,......不是XCode。XCode 是您在其中开发 Objective-c 的应用程序。就像 Netbeans 或 Eclipse 通常用于 Java 和 PHP。