创建不在模拟器中显示的图像帧

时间:2014-08-08 02:32:27

标签: ios uiviewcontroller cgrect

我有一个捕获用户签名的应用程序,myviewcontroller显示签名捕获。我尝试在屏幕中心创建一个矩形,该矩形使用CGRect进行居中。当我运行这个时,我没有错误,但我得到了下面的图片,我无法弄清楚是什么导致了这一点。 在CGRect代码中,我试图让创建的矩形在屏幕中间居中,目标设备是ipad。我发布了.h和.m文件,.m文件不完整,因为其余部分与用于捕获签名的代码有关,然后保存。

任何帮助将不胜感激。 Simulation

// MyViewController.h

    #import <UIKit/UIKit.h>
    #import <CoreData/CoreData.h>
    #import <Foundation/Foundation.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 (nonatomic, assign) UIImageView* imageView;


    @property (nonatomic, retain) NSData*image;
    @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

    //Save and cancel buttons

    - (IBAction)cancel:(id)sender;
    - (IBAction)save:(id)sender;


    @end

//MyViewController.m

#import "MyViewController.h"



@interface MyViewController ()

@end

@implementation MyViewController

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


- (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 imageFrame = CGRectMake(
                                   CGRectGetMidX(self.view.frame) - CGRectGetMidX(imageView.frame),
                                   CGRectGetMidY(self.view.frame) - CGRectGetMidY(imageView.frame),
                                   CGRectGetWidth(imageView.frame),
                                   CGRectGetHeight(imageView.frame));

//    //allocate an image view and add to the main view
    mySignatureImage = [[UIImageView alloc] initWithImage:nil];
    mySignatureImage.frame = imageFrame;
    mySignatureImage.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mySignatureImage];


}

2 个答案:

答案 0 :(得分:0)

尝试替换此行:

mySignatureImage = [[UIImageView alloc] initWithImage:nil];

使用:

mySignatureImage = [[UIImageView alloc] init];

答案 1 :(得分:0)

[super viewDidLoad]中的代码(viewDidLoad除外)移至后一阶段,例如viewWillAppear。在viewDidLoad期间,视图的布局尚未完成,因此它们的边界/框架尚未设置为最终值。