UIImage loaded from NSData shows up in black only on new iPad 3

I have a generic binary app that is currently published on iTunes and works correctly on iPhone 3, 4, 4S, iPad 2 on iOS 4.x up to 5.1, but displays a black UIImage on a new iPad (3) running iOS 5.1.

Has anyone else encountered this? Any suggestions for a fix?

The problem is that the UIImage loaded from NSData shows up as black.

I originally thought NSData might be nil or unreadable, so I added a UIAlert and NSLog to output the NSData length and UIImage size properties (assuming that reading the data into UIImage would result in reasonable size values). Everything looks fine in UIAlert ...

// MyView.m

- (NSData*)getImageData

{      

     Screening *screen = [[Model sharedInstance] screenInProgress];

     return screen.anteriorImage;  // returns the NSData from the managed 'Screening' object

}


- (void)viewWillAppear:(BOOL)animated

{
     NSData *imagedata = [self getImageData];

     UIImage *image = [UIImage imageWithData:imagedata];

     [clientImageView setImage:image];     /// UIImageView in nib




     /// apply user grid
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


     if ([defaults integerForKey:@"com.ikonetics.posture.gridoverlay"] >= 0) {     

          UIImageView *grid = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grid.png"]];

          [grid setBackgroundColor:[UIColor clearColor]];

          [grid setAlpha:0.6f];

          [grid setFrame:clientImageView.frame];

          [screenshotWrapper addSubview:grid];     // screenshotWrapper is a UIView in xib arranged as a parent to other views

          [grid release];

     }


     NSString *note = [NSString 

                           stringWithFormat:@"NSData length: %d \nUIImage size: %@ \nUIImage scale: %1.f \nClientImage info: %@"

                           , [imagedata length]

                           , NSStringFromCGSize(image.size)

                           , image.scale

                           , [clientImageView description]

                           ];



     NSLog(@"debug note: %@ ", note);

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DEBUG INFO" 

                                                                 message:note

                                                               delegate:nil

                                                    cancelButtonTitle:@"OK"

                                                    otherButtonTitles:nil];

     [alert show];   

     [alert release];



}



// Screening.h

@interface Screening : NSManagedObject


@property (nonatomic, retain) NSData * anteriorImage;     // this data is stored elsewhere using UIImageJPEGRepresentation(image, 0.8);
...
@end

      

Screenshot from iPad 3 showing the UIAlertView, you can see that NSData is long, and UIImage is sized, and UIImageView (ClientImage) has a reasonable frame, and the UIImage grid on the grid displays correctly as expected.

  • The returned NSData are long and not nil

    (187433 below)
  • UIImage was able to analyze the data and construct an image, evidence for the size and properties of the scale (784, 980).

iPad 3 screenshot

Finally, this was tested with the option disabled /// apply user grid

, so the png overlay is not even generated as this codeblock does not run. The problem with black UIImage persists.

Anyone else have problems with UIImageView showing black UIImage instead of actual image? Any thoughts or ideas on how to fix the code to make this work correctly on iPad 3?

Thank!

EDIT - Additional information for debugging:

I wrote NSData (imagedata) via email so that I can view it from the device. I wanted to see if it was possible that the image was blank / black and found out that the data is displayed correctly as an image when saved from the device (it is not blank).

This way NSData loaded in UIImage and UIImageView is displayed in black ... but when attached to email, it displays the image correctly.

Also, I wanted to make sure the view hierarchy is displayed as intended; make sure my image is not under any other guise. Starting with ViewController view

, I registered recursiveDescription

and found out that the view hierarchy on iPad 3 is the same as iPad 2, and this is how I expect it.

New debug code:

NSString *note = [NSString 
                  stringWithFormat:@"NSData length: %d \nUIImage size: %@ \nUIImage scale: %1.f \nRecursive info: %@"
                  , [imagedata length]
                  , NSStringFromCGSize(image.size)
                  , image.scale
                  , [[self view] recursiveDescription]
                  ];

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"Debug email"];
[mailViewController setMessageBody:note isHTML:NO];
[mailViewController addAttachmentData:imagedata mimeType:@"image/jpg" fileName:@"imagedata.jpg"];

[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];

      

Finally, here is the recursive log information from two and three devices:

iPad 2:

NSData length: 123254 
UIImage size: {800, 960} 
UIImage scale: 1 
Recursive info: <UIView: 0x10a91620; frame = (0 0; 768 960); autoresize = W+H; layer = <CALayer: 0x10a91650>>
   | <UIScrollView: 0x10a90a10; frame = (0 0; 768 960); clipsToBounds = YES; autoresize = RM+TM; layer = <CALayer: 0x10a90bb0>; contentOffset: {0, 0}>
   |    | <UIView: 0x10a90be0; frame = (0 0; 1536 1920); autoresize = LM+RM+TM+BM; layer = <CALayer: 0x10a90c10>>
   |    |    | <UIView: 0x10a90c40; frame = (384 480; 768 960); autoresize = LM+RM+TM+BM; layer = <CALayer: 0x10a90c70>>
   |    |    |    | <UIImageView: 0x10a908f0; frame = (0 0; 768 960); opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10a90930>>
   |    |    |    | <LineView_iPad: 0x10a90ce0; frame = (0 0; 768 960); autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10a90d20>>
   |    |    |    | <PointView_iPad: 0x10a90dd0; frame = (0 0; 768 960); autoresize = LM+RM+TM+BM; layer = <CALayer: 0x10a90e20>>
   |    |    |    | <UIImageView: 0x10a96480; frame = (0 0; 768 960); alpha = 0.6; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x10a966e0>>
   | <UIView: 0x10a91680; frame = (-10 750; 220 220); autoresize = RM+TM; layer = <CALayer: 0x10a916b0>>
   |    | <UIImageView: 0x10a916e0; frame = (0 0; 220 220); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x10a91720>>
   |    | <UIImageView: 0x10a91480; frame = (10 10; 200 200); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10a914c0>>
   |    | <UIImageView: 0x10a91840; frame = (102 102; 16 16); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10a91880>>

      

iPad 3:

NSData length: 166252
UIImage size: {784, 980}
UIImage scale: 1
Recursive info: <UIView: 0xc669470; frame = (0 0; 768 980); autoresize = W+H; layer = <CALayer: 0xc667550>>
 | <UIScrollView: 0x33d410; frame = (0 20; 768 960); clipsToBounds = YES; autoresize = RM+TM; layer = <CALayer: 0x325840>; contentOffset: {0, 0}>
 |    | <UIView: 0x308630; frame = (0 0; 1536 1920); autoresize = LM+RM+TM+BM; layer = <CALayer: 0xc666f40>>
 |    |    | <UIView: 0x318970; frame = (384 480; 768 960); autoresize = LM+RM+TM+BM; layer = <CALayer: 0xc682a70>>
 |    |    |    | <UIImageView: 0xc66cd60; frame = (0 0; 768 960); opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xc672c00>>
 |    |    |    | <LineView_iPad: 0x301680; frame = (0 0; 768 960); autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x329fa0>>
 |    |    |    | <PointView_iPad: 0xc6841b0; frame = (0 0; 768 960); autoresize = LM+RM+TM+BM; layer = <CALayer: 0xc672b10>>
 |    |    |    | <UIImageView: 0xc588b70; frame = (0 0; 768 960); alpha = 0.6; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xc589500>>
 | <UIView: 0x324330; frame = (-10 770; 220 220); autoresize = RM+TM; layer = <CALayer: 0xc687970>>
 |    | <UIImageView: 0xc665b80; frame = (0 0; 220 220); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x321490>>
 |    | <UIImageView: 0x314e50; frame = (10 10; 200 200); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xc684f30>>
 |    | <UIImageView: 0x63d220; frame = (102 102; 16 16); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xc580d90>>

      

Even though this post is so unbearably long, I hope someone out there can think of anything that could be causing this problem ...

+3


source to share


1 answer


This seems to be a bug with iOS. This is a workaround:

  • In the Builder interface change the background texture of the UIImageView to any color

What is it.

I came across this while debugging, randomly flipping the switches while trying to achieve something interesting. My code / app is not using the background color of the UIImageView, its property has not been configured ... ever?



In my code above, the UIImageView

'clientImageView' had its background set with the "View background color with background" texture. With this texture set as the background, the loaded UIImage refused to display. Changing the background texture to anything but Flipside and the UIImage displays as expected.

  • The only change we made is the background color property of the UIImageView in the interface builder.
  • The ONLY location of the error was on an iPad 3 running iOS 5.1.
  • It fails every time and reproduces.
  • also tested on phone 4, 4S, iPad 2 on iOS 4.x through 5.1

Reported by Apple as Bug ID # 11080928

+2


source







All Articles