How to set the resolution of a saved image from the UIImagePickerController.

Discussion in 'iOS Programming' started by BarryK88, Apr 7, 2011.

  1. macrumors member

    Joined:
    Mar 1, 2011
    #1
    I'm using an UIImagePickerController to set a picture from my cameraroll to a UIImageView. However the image is getting scaled automaticly inside this UIImageView because I mentioned 'scale to fit' inside the IB. I'd like to save my chosen image with a resolution of 80x80 pixels so that I won't have to scale. (My App is getting realy slow because of this scaling issue.)

    Here's a snippit from my code:

    Code:
    -(void)imagePickerController:(UIImagePickerController *)picker
        	  didFinishPickingImage : (UIImage *)image
        				 editingInfo:(NSDictionary *)editingInfo
        {
        	[picker dismissModalViewControllerAnimated:YES];
        	UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        		photoView.image = image;
        	
        
        		NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];		
        		NSString *pngFilePath = [NSString stringWithFormat:@"%@/foto1.png",docDir];
        		NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
        		
        		[data writeToFile:pngFilePath atomically:YES];
        }
    Help is greatly appreciated!
     
  2. ChOas, Apr 7, 2011
    Last edited: Apr 7, 2011

    macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #2
    I can't check this right now, but maybe something like this ?

    Code:
    - (UIImage *) thumbnailFromImage:(UIImage *)image {
    
        CGRect imageRect = CGRectMake(0, 0, 80, 80);
    
        UIGraphicsBeginImageContext(imageRect.size);
    
        [image drawInRect:imageRect];
    
        UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return thumbnail;
    }
    
    
    Something like that should work, I think ?
     

Share This Page