Ios – how to align the image in the center IOS horizontally

iosios5objective c

Here is my image :

Obj *obj = ... obj has imageHref which is NSString

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; 
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

Depending on image size I can't figure out how to center it, any ideas ?

This is all inside method :

- (void)drawRect:(CGRect)rect {    
    Obj *obj = ... obj has imageHref which is NSString

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; 
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

    NSLog(@"%f", rect.size.width);
     NSLog(@"%f", rect.size.height);
}

Where rect.size.width prints 320.000000 and rect.size.height prints 416.000000

Best Answer

[image drawInRect:CGRectMake((self.view.frame.size.width/2) - (image.size.width/2),
                             (self.view.frame.size.height / 2) - (image.size.height / 2),
                             image.size.width, 
                             image.size.height)];

edit:

As you are already in a UIView:

[image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)];