问题
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
ivas a subview ofself.imageViewinstead of just changing the image property ofself.imageView. Knowing this could be useful to answer properly. - Supposing you have a good reason for adding
ivas a subview, bewareiv.center = self.imageView.centerthe 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.
- Does
self.imageViewexist? 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 notnil - Test: try
self.imageView setImage:iminstead ofself.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 ofUIImage* im = UIGraphicsGetImageFromCurrentImageContext();
andCGContextDrawImage(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