move didFinishLaunchingWithOptions: method to viewcontroller method with button trigger

岁酱吖の 提交于 2019-12-24 21:02:59

问题


I had the following code working in AppDelegate.h/.m but cannot change it to work in ViewController with a button triggering it.

@implementation BSViewController

@synthesize imageView;
@synthesize workingImage;
- (IBAction) chooseImage:(id) sender {

    UIImage* testCard = [UIImage imageNamed:@"ipad 7D.JPG"];
    CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
    UIGraphicsBeginImageContext(CGSizeMake( 250,650));
    CGContextRef con = UIGraphicsGetCurrentContext();
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
    UIGraphicsEndImageContext();
    CGImageRelease(num);

    UIImageView* iv = [[UIImageView alloc] initWithImage:im];
    [self.imageView addSubview: iv];
    iv.center = self.imageView.center;
    [iv release];

I see the button named "Choose image" in the simulator, but I do not see the image there.


回答1:


You haven't added self.imageView to any views. You need

[self.view addSubview:self.imageView];

To show your imageView.




回答2:


I was able to make it work, but let me ask you some dummy questions before. You can skip these and jump directly to the end for the answer:

  • Did it worked before?
  • Why aren't you using ARC?
  • Have you linked the chooseImage: to the button with Interface Builder, or programmatically? I sometimes forget this basic step, and nothing works of course :)
  • Why are you adding iv as a subview of self.imageView instead of just changing the image property of self.imageView. Knowing this could be useful to answer properly.
  • Supposing you have a good reason for adding iv as a subview, beware iv.center = self.imageView.center the centers are in two different coordinates system.
    Form the Apple Documentation:

    The center is specified within the coordinate system of its superview and is measured in points. Setting this property changes the values of the frame properties accordingly.

Edit: Other things to check
  • Does self.imageView exist? I mean, have you added it with IB and linked it to the IBOutlet, or defined it programmatically?
  • Add a break point in chooseImage: and go through it step by step, making sure that the objects you generate there are not nil
  • Test: try self.imageView setImage:im instead of self.imageView addSubview:iv, even if it's not what you want to do, if everything else is ok you should see the image.

It works if you switch the order of
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
and
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);

Otherwise you would be asking to create im with the content of the context without nothing in drawn in it.

The code would then become:

UIImage* testCard = [UIImage imageNamed:@"ipad 7D.JPG"];
CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
UIGraphicsBeginImageContext(CGSizeMake( 250,650));
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(num);

Hope it helps. :D



来源:https://stackoverflow.com/questions/14305112/move-didfinishlaunchingwithoptions-method-to-viewcontroller-method-with-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!