1

I'm working in the simulator right now and when trying to access the attributes of an image returned by UIImagePickerController's didFinishPickingImage, I get an EXC_BAD_ACCESS error:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    NSLog(@"Image size: %@", img.size);
}

What am I doing wrong?

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
Jon Hinson
  • 498
  • 4
  • 6

3 Answers3

5

You are trying to print object ("%@" format specifier), but size has CGSize type which is typedef to plain struct. Use

NSLog(@"Image size: %@", NSStringFromCGSize(img.size));

or

NSLog(@"Image size: (%f, %f)", img.size.width, img.size.height);
hoha
  • 4,418
  • 17
  • 15
  • 1
    +1 I wasn't aware of the [NSStringFromCGSize](http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/NSStringFromCGSize) function, very cool. Prints size in the form {w,h} – theChrisKent Mar 14 '11 at 15:24
0

UIImage.size is a structure, not an object, so you can't use %@ in NSLog to log it. Try this instead:

NSLog(@"Image size: %.0f %.0f", img.size.width, img.size.height);
Greg
  • 33,450
  • 15
  • 93
  • 100
0

Do this instead:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    NSLog(@"Image size: %f by %f", img.size.width, img.size.height);
}
theChrisKent
  • 15,029
  • 3
  • 61
  • 62