4

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.

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.

Frostmourne
  • 156
  • 1
  • 19
chrisw
  • 273
  • 1
  • 4
  • 11
  • xcode is just an editor. – vikingosegundo Apr 26 '12 at 13:40
  • Thanks for the edit very tired. Thanks for the ideas suggested so far. They are pretty similar to what I'm using at the moment and I end up with an image of size 320x426 when I view it on the server – chrisw Apr 26 '12 at 13:58
  • Are you sure nothing is wrong with the connection? I mean, if the upload to your server gets disturbed, it leaves you with an uncompleted image upload, however, that would mainly affect the heihgt of the image. – Paul Peelen Apr 29 '12 at 17:57
  • Realised I was doing some stupid stuff server-side causing my images to be resized again regardless of what I tried. All sorted now, found quite a good tutorial here:[link](http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/) which really helped. Thanks everyone for your help I tried a lot of your solutions but because of my own stupidity I can't say which ones worked or not. – chrisw Apr 29 '12 at 22:24

3 Answers3

5
-(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.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
Anshuk Garg
  • 1,540
  • 12
  • 14
  • If I send the image retrieved from this method I get an image of 320x426 when I send it to the server, it seems it only goes to the size of the screen like a screenshot. – chrisw Apr 26 '12 at 14:17
  • I get EXC_BAD_ACCESS on CGBitmapContextCreate – chrisw Apr 26 '12 at 15:22
  • I found that this way has a memory leak afterwards iOS 5.0 see the following! http://stackoverflow.com/a/4712537/1752988 – Randika Vishman Apr 15 '14 at 20:11
0

// 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...

Nitin
  • 7,455
  • 2
  • 32
  • 51
0

Maybe this post helps you out: How to resize the image programmatically 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.

Community
  • 1
  • 1
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168