从NSData加载的UIImage仅在新iPad 3上显示黑色

时间:2012-03-19 14:42:11

标签: ios ios5 uiimage nsdata ipad-3

我有一个通用二进制应用程序,目前已在iTunes上发布,可在iOS 4.x到5.1的iPhone 3,4,4S,iPad 2上正常运行,但在运行iOS的新iPad(3)上显示黑色UIImage 5.1。

还有其他人遇到过这个吗?任何修复建议?

问题是从NSData加载的UIImage显示为黑色。

我最初认为NSData可能是零或不可读的,所以我添加了UIAlert& NSLog输出NSData长度和UIImage大小属性(假设成功读取数据到UIImage将产生合理的大小值)。 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

来自显示UIAlertView的iPad 3的屏幕截图,您可以看到NSData具有长度且UIImage具有大小且UIImageView(ClientImage)具有合理的框架,并且网格覆盖UIImage正确显示,如预期的那样。

  • 返回的NSData有长度而不是nil (下面是187433)
  • UIImage能够解析数据并构建图像,大小和比例属性的证据(784,980)

iPad 3 screenshot

最后,这已经在禁用/// apply user grid选项的情况下进行了测试,因此叠加png甚至不会被实例化,因为该代码块不会运行。黑色UIImage问题仍然存在。

其他任何有UIImageView问题的人显示黑色UIImage而不是实际图像?有关如何修复代码的任何想法或想法,以便在iPad 3上正常工作吗?

谢谢!

编辑 - 更多调试信息:

我将NSData(imagedata)写入电子邮件,以便我可以在设备上查看它。我想知道图像本身是否为空白/黑色,并了解到数据在从设备上保存时正确显示为图像(它不是空白的)。

因此,NSData加载到UIImage& UIImageView显示黑色...但当附加到电子邮件时,它会正确显示图像。

此外,我想确保视图的层次结构按预期显示;检查我的图像是否在另一个视图下面。从ViewController的view开始,我记录了recursiveDescription并了解到iPad 3上的视图层次与iPad 2相匹配,这正是我所期望的。

新的调试代码:

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];

最后,这是来自2&的递归日志信息。 3台设备:

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>>

尽管这篇帖子太长了,但我希望有人可以想到任何可能引起这个问题的事情......

1 个答案:

答案 0 :(得分:2)

这似乎是iOS的一个错误。这是解决方法:

  • 在Interface Builder中,将UIImageView背景纹理更改为任何颜色

就是这样。

我在调试期间偶然发现了这一点,同时任意翻转开关试图让有趣的事情发生。我的代码/应用程序没有使用UIImageView的背景颜色,其属性尚未调整......永远不会?

在上面的代码中,UIImageView'clientImageView'的背景设置为纹理“View Flipside Background Color”。将该纹理设置为背景,加载的UIImage拒绝显示。将背景纹理更改为除Flipside之外的任何内容,并按预期显示UIImage。

  • 我们所做的唯一更改是在Interface Builder中使用UIImageView的背景颜色属性。
  • 出现错误的唯一地方是运行iOS 5.1的iPad 3硬件。
  • 每次都失败并且可重现
  • 还在iOS 4.x到5.1
  • 上的Phone 4,4S,iPad 2上进行了测试

向Apple报告错误ID#11080928