Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

save and retrieve image from parse

does anyone knows the best way to make the image smaller so when I am trying to load it back in the app it wont take a long time to load

i try both save it as png or jpeg still it takes to long .

1 Answer

Hello,

what is use is a category of UIImage and takes a normal image and a CGSize so here it is,

.h

+ (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;

.m

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

    return newImage;
}

and an example is.

imageView.image = [UIImage imageWithImage:[UIImage imageWithData:data] scaledToSize:CGSizeMake(40, 40)];

this is what I do and I quite like it :)

Best, Kai

Mmm no, make a new category on UIImage, add this code, import it the you can call imageWithImage: scaleToSize: which is a convinient block of code to have. So no, you don't need to change your code, just add this add a category on UIImage and you now have extended functionality in your app for resizing images.

you add that where every you need the resized image :)

well you can put it there, where you would put it, is take the image they selected, resize it to your needs then save it to cut down on networking time. or use this code ,

UIImageJPEGRepresentation(img, 1.0f)

this is another super helpful tool that I also use a lot, what you do is pass in an image, then set it's resolution (in my case it's full because 1.0 is 100%). side note: this will convert it to a PNG and there isn't a PNG version of this method.

I did exactly what you say i create the category as UIImage then save the way you say , nothing happened in my tableview its still not loading Kai Aldag