调整图像的大小,Objective-C [重复]

问题描述 投票:3回答:3

此问题已经在这里有了答案:

我正在尝试缩小用iPhone 4及更高版本在Objective-C中拍摄的图像,以便可以将其尽快发送到服务器。目前,完整尺寸的图像正在老化。

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

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);

这给了我一张320x460的图像,我如何获得它想要的尺寸484x888。

ios objective-c uiimage uikit
3个回答
4
投票
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

使用此方法缩小图像。


0
投票

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

 -(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;
}

希望,这对您有帮助...


0
投票

也许这篇文章可以帮助您:How to resize the image programmatically in objective-c in iphone

请注意...您正在Objective-C中进行此操作,...而不是XCode。 XCode是您在其中开发Objective-c的应用程序。就像Netbeans或Eclipse通常用于Java和PHP。

© www.soinside.com 2019 - 2024. All rights reserved.