7

I am creating an image frame using CGRect. I want to center the rectangle that is created.

I've looked on here, as well as in the Apple documentation for the best way to do this, and found CGRectGetMidY and CGRectGetMidX which get the center coordinates.

When I try implementing this into my own code I run into problems. I get a Property size not found on object of type UIIMageView error

       #import "MyViewController.h"



    @interface MyViewController ()

    @end

    @implementation MyViewController

    @synthesize mySignatureImage;
    @synthesize lastContactPoint1, lastContactPoint2, currentPoint;
    @synthesize imageFrame;
    @synthesize fingerMoved;
    @synthesize navbarHeight;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];



        self.view.backgroundColor = [UIColor lightGrayColor];

        CGRect mySignatureImageFrame = CGRectMake(
                                       CGRectGetMidX(self.view.frame) - (mySignatureImage.size.width/ 2.0),
                                       CGRectGetMidY(self.view.frame) - (mySignatureImage.size.height / 2.0),
                                       image.size.width,
                                       image.size.height);




#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UIAlertViewDelegate>

@property (nonatomic, strong) UIImageView *mySignatureImage;
@property (nonatomic, assign) CGPoint lastContactPoint1, lastContactPoint2, currentPoint;
@property (nonatomic, assign) CGRect imageFrame;
@property (nonatomic, assign) BOOL fingerMoved;
@property (nonatomic, assign) float navbarHeight;


@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
aramirez
  • 87
  • 1
  • 1
  • 8

5 Answers5

11

Assuming image is of type UIImage then:

CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - (image.size.width / 2.0),
    CGRectGetMidY(self.view.frame) - (image.size.height / 2.0),
    image.size.width,
    image.size.height);

Assuming imageView is of type UIImageView then:

CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - CGRectGetMidX(imageView.frame),
    CGRectGetMidY(self.view.frame) - CGRectGetMidY(imageView.frame),
    CGRectGetWidth(imageView.frame),
    CGRectGetHeight(imageView.frame));
Droppy
  • 9,691
  • 1
  • 20
  • 27
  • Thanks for your comment, when I try this I get a "Property 'size' not found on object of type 'NSData' – aramirez Aug 06 '14 at 10:25
  • @aramirez My answer assumes `image` is an `UIImage` object. You didn't show what the image was in your question so you'll have to expand the code in your question in order for me to answer more completely. – Droppy Aug 06 '14 at 10:27
  • I've updated my code snippet above for your reference, thank you – aramirez Aug 06 '14 at 11:02
  • Thank you for updating, so when I make thise changes, I get a semantic error, that says no member named frame in struct cgrect – aramirez Aug 06 '14 at 11:26
  • Not anymore, thanks for the update it compiles but when i run the simulator I dont see the rectange it's making at all – aramirez Aug 07 '14 at 00:33
  • @aramirez OK, post another question if you cannot solve that issue. – Droppy Aug 07 '14 at 05:20
7

You don't need to calculate the centre point, as it's available for a view anyway:

CGRect superviewBounds = superview.bounds;
imageView.center = CGPointMake(CGRectGetMidX(superviewBounds), CGRectGetMidY(superviewBounds));
Joel
  • 15,654
  • 5
  • 37
  • 60
Anorak
  • 3,096
  • 1
  • 14
  • 11
  • @holex Not really. `bounds` and `center` are what are actually used to set a view's `frame`. So centering an image or putting an image anywhere you want is just as easily done by setting the `center` property of a view to any point that you want it to be at. – Anorak Aug 06 '14 at 11:09
  • if you have two views: `view1` with frame `(100.0, 100.0, 50.0, 30.0)` and it has a subview as `view2` with frame `(0.0, 0.0, 50.0, 30.0)` the `view2.center` will never be equal to the `view2.superview.center`... your idea works only if the superview covers the entire screen precisely – in any other cases it won't work as you'd like to. – holex Aug 06 '14 at 11:13
1
- (void)viewDidAppear:(BOOL)animated {


img.center = CGPointMake(CGRectGetMidX([self.view bounds]), CGRectGetMidY([self.view   bounds]));
}

/*write code in viewDidApper */
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
Mirant
  • 308
  • 2
  • 13
0
CGPoint (^CGRectGetCenter)(CGRect) = ^(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
};

example : The above answer by Mirant

Suraj K Thomas
  • 5,773
  • 4
  • 52
  • 64
0
let center = CGPoint(x: rect.midX, y: rect.midY)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66