user3781174
user3781174

Reputation: 235

Resize an Image Inside a UIImage before saving in directory

I have this code which gets the image inside my UIImage (connected with IB called 'c1_1') and should save in my directory folder:

IBOutlet UIImageView *c1_1;

if (c1_1.image != nil)
        {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/ImagensApp/ImagensClientes/%@_%@_Cliente.jpg",list[0],abc]];
            NSData* data = UIImageJPEGRepresentation(c1_1.image, 70);
            [data writeToFile:path atomically:YES];
        }

This code works, but I want to resize the image to scale 200x200, how I can do this before saving?

Upvotes: 0

Views: 479

Answers (1)

user3386109
user3386109

Reputation: 34839

Scaling an image is just a few lines of code that

  • creates a memory bitmap
  • draws the image into that bitmap
  • and then gets the new image from the bitmap

like this

- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake( 0, 0, newSize.width, newSize.height )];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return( result );
}

Upvotes: 1

Related Questions