0

In my application i have image with dimension 1936*2592 and size upto 6 MB. i need scale image to 0.25 or resize image to 478*640 and then upload it to server. I have tried lots of ways to do it in ios6 but failed

imageData= UIImagePNGRepresentation(self.cl_PriviewImage);
UIImage *temp = [[UIImage alloc] initWithData: imageData];
ScaledImage = [temp resizedImage:CGSizeMake(478, 640) interpolationQuality:kCGInterpolationHigh];
ScaledData = UIImagePNGRepresentation( ScaledImage);

//self.cl_PriviewImage is my image it shows resizedImage method not found. I have also tried with ScaleToSize method but its showing same error.

i have also tried with

ScaledImage = [UIImage imageWithCGImage:self.cl_PriviewImage.CGImage scale:0.25f orientation:self.cl_PriviewImage.imageOrientation];
ScaledData = UIImagePNGRepresentation(ScaledImage);

in this case there is no error but my purpose didn't get fulfilled i.e uploaded image doesn't get changed in scale/size.

Is there any any other way in iOS 6 or am i wrong some where? Please suggest me. Thanks in advance

Ajit Satarkar
  • 717
  • 6
  • 20

1 Answers1

0
- (UIImage*) image:(UIImage *) image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1);
    CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationHigh);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

You can also add this method in a UIImage category, that would be a better solution.

Andrey Zverev
  • 4,409
  • 1
  • 28
  • 34
  • Hi is there any way that the image it returns should be with transparent background?.. – Ajit Satarkar Oct 26 '12 at 10:39
  • I have tried with UIGraphicsBeginImageContextWithOptions(newSize, YES, 1); then it just covert background from white to black.. – Ajit Satarkar Oct 26 '12 at 10:40
  • Or could it return image in PNG format? now it returns image in JPG format – Ajit Satarkar Oct 26 '12 at 11:15
  • 1
    @AjitSatarkar add `CGContextRef context = UIGraphicsGetCurrentContext();`, `CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);` and `CGContextFillRect(context, CGRectMake(0,0,newSize.width,newSize.height));` before `[image drawInRect:]`. – Andrey Zverev Oct 26 '12 at 14:40
  • @AjitSatarkar and `UIImage` has no "format" before you get a JPG or PNG representation from it. – Andrey Zverev Oct 26 '12 at 14:42
  • Ok Thanks I'll definitely try this tomorrow in office. Well yesterday what i did was - – Ajit Satarkar Oct 28 '12 at 03:32
  • - (UIImage) image(UIImage ) image scaledToSize(CGSize)newSize; { UIGraphicsBeginImageContextWithOptions(newSize, NO, 1); CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationHigh); [image drawInRectCGRectMake(0,0,newSize.width,newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); ScaledData=UIImagePNGRepresentaion(newImage); ScaledImage=[UIImage imageWithData:ScaledData] return ScaledImage; } – Ajit Satarkar Oct 28 '12 at 03:43
  • ScaledImage,ScaledData are objects of UIImage and NSData declared as Property in same classs. This works it returns image in PNG format which keeps my scaled image transparency as it is. Thanks – Ajit Satarkar Oct 28 '12 at 03:43