【问题标题】:<Error>: CGImageCreate: invalid image size: 0 x 0<错误>:CGImageCreate:无效的图像大小:0 x 0
【发布时间】:2014-06-02 12:54:45
【问题描述】:

在给出重复标记之前阅读我的整个问题:

我知道可能有很多与我的标题相关的问题,但问题不同。

我正在使用 Mac OS X 10.9.3 和 Xcode 5.1.1

在我的应用程序中,我只想在 Facebook 和 Twitter 上分享图片、URL 链接和一些描述,所以我使用了SLComposeViewController,我的代码如下。

-(void)btnShareTapped:(UIButton *)sender
{
  if(sender.tag == 100)
    [self shareProductContentOnTwitterORFB:@"Facebook"];
  else 
    [self shareProductContentOnTwitterORFB:@"Twitter"];   
}

方法体:

-(void) shareProductContentOnTwitterORFB:(NSString *) shareON
{
    SLComposeViewController *shareKit =  nil; // also tried with [[SLComposeViewController alloc] init];
    if([shareON isEqualToString:@"Twitter"])
        shareKit = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    else
        shareKit = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [shareKit setInitialText:@"My Description Text."];
    [shareKit addImage:[UIImage imageNamed:@"boston-shopping.jpg"]]; // I also tried with .png image
    [shareKit addURL:[NSURL URLWithString:@"www.google.com"]];
    //[self.view.window.rootViewController presentViewController:shareKit animated:YES completion:nil];
    [self presentViewController:shareKit animated:YES completion:nil]; /// I also tried with above comment.
}

执行上述代码后,SLComposeViewController 将打开并在我的控制台中

<Error>: CGImageCreate: invalid image size: 0 x 0.

仅使用 Facebook 显示并得到完全相同的问题Look at this question. 我确定我的图片不是nil,我已经检查过了。

上面的问题说——

这个错误似乎已在新版本的 iOS (6.0.1) 中得到修复,至少自上次升级以来我一切正常。

但我在 iOS 6.1 上进行测试仍然遇到同样的问题,并且在 iOS 7.0 和 7.1 控制台中出现错误消息(&lt;Error&gt;: CGImageCreate: invalid image size: 0 x 0.) 不显示,但我无法在 Facebook 和 Twitter 中设置应用程序。
什么是问题?我错在哪里?这是苹果Bug吗??
请提出您的建议。

【问题讨论】:

  • 你可以试一试吗:将SLComposeViewController声明为属性并查看。
  • @Maulik - 试过了,不适合我:(
  • 我想得到你的代码:)
  • 所以尝试注释掉添加图像的行 - 有什么变化吗?尝试在 CGImageCreate() 上放置一个断点并查看堆栈的内容。如果做不到这一点,请构建一个有问题的小型演示应用程序,将其放在 Dropbox 上,并提供赏金以进行适当的修复。
  • 重现该问题的最小项目将有助于...

标签: ios objective-c facebook cocoa-touch slcomposeviewcontroller


【解决方案1】:

在我调用[self presentViewController:shareKit animated:YES completion:nil]; 后看看这个堆栈跟踪:

* thread #1: tid = 0x199f3, 0x0026a871 CoreGraphics`CGPostError, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0026a871 CoreGraphics`CGPostError
    frame #1: 0x0021bfc0 CoreGraphics`CGImageCreate + 296
    frame #2: 0x00458ea5 UIKit`_UICreateCGImageFromCARenderServerBuffer + 201
    frame #3: 0x007e7b0e UIKit`-[UISnapshotView captureSnapshotRect:fromView:withSnapshotType:] + 1712
    frame #4: 0x008fdfa7 UIKit`-[_UIRemoteViewController _snapshotAndRemoveTextEffectsRemoteView] + 337
    frame #5: 0x008fe064 UIKit`-[_UIRemoteViewController _applicationWillResignActive:] + 33
    frame #6: 0x00f9a4f9 Foundation`__57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 40
    frame #7: 0x014e50c5 CoreFoundation`___CFXNotificationPost_block_invoke_0 + 85
    frame #8: 0x0143fefa CoreFoundation`_CFXNotificationPost + 2122
    frame #9: 0x00ecebb2 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 98
    frame #10: 0x0041abb1 UIKit`-[UIApplication _deactivateForReason:notify:] + 381
    frame #11: 0x0041ac3d UIKit`-[UIApplication _deactivateForReason:] + 48
    frame #12: 0x0041cfd4 UIKit`_alertItemStateChanged + 73
    frame #13: 0x014e52e3 CoreFoundation`__CFNotificationCenterAddObserver_block_invoke_0 + 163
    frame #14: 0x014e53a0 CoreFoundation`____CFXNotificationPostToken_block_invoke_0 + 176
    frame #15: 0x0144a920 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    frame #16: 0x0140dd31 CoreFoundation`__CFRunLoopDoBlocks + 257
    frame #17: 0x01431c51 CoreFoundation`__CFRunLoopRun + 2273
    frame #18: 0x01430f44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #19: 0x01430e1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #20: 0x01dcb7e3 GraphicsServices`GSEventRunModal + 88
    frame #21: 0x01dcb668 GraphicsServices`GSEventRun + 104
    frame #22: 0x0041dffc UIKit`UIApplicationMain + 1211
    frame #23: 0x00002f3d iostest`main(argc=1, argv=0xbffff0d0) + 141 at main.m:16

我试图在我的示例项目中运行你的代码,我发现了这些:

  • [self presentViewController:shareKit animated:YES completion:nil]; 之后和模态shareKit 视图出现之前打印了错误消息。
  • 错误消息是从CGPostErrorCoreGraphics 中打印的。
  • 我认为出现此错误是因为-[UISnapshotView captureSnapshotRect:fromView:withSnapshotType:]
  • 我试图注释掉你的这一行,[shareKit addImage:[UIImage imageNamed:@"boston-shopping.jpg"]];,但我仍然收到错误消息。所以,错误不是因为你的代码。

在我看来,我认为您收到的错误消息是因为 UISnapshotView 试图拍摄当前屏幕的快照,但那里什么也没有,或者该方法存在一些错误。因此,它将错误的 width = 0 &amp; height = 0 参数传递给了导致错误的 CGImageCreate

所以,请放心,这不是您的错误。忽略它。

【讨论】:

    【解决方案2】:

    尝试自己创建CGImageRef 对象。你可以换行:

    [shareKit addImage:[UIImage imageNamed:@"boston-shopping.jpg"]];
    

    用这个:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"boston-shopping" ofType:@"jpg"];
    CGDataProviderRef source = CGDataProviderCreateWithFilename([filePath cStringUsingEncoding:NSUTF8StringEncoding]);
    CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(source,
                                                            NULL,
                                                            true,
                                                            kCGRenderingIntentDefault);
    [shareKit addImage:[UIImage imageWithCGImage:imageRef]];
    CGImageRelease(imageRef);
    

    【讨论】:

    • 我试过你的代码,它不适合我。同样的问题..它可能是苹果的错误仍然没有解决.. :(
    • 每个UIImage 内部都有一个CGImage。这样你只是在浪费数据。
    • @Sulthan 我不明白您对此代码的问题。我在这里所做的只是提供一种通过CGImage 手动创建UIImage 对象的方法。这里没有浪费。
    • @GuyKogus 这比+[UIImage imageNamed:] 好多少?你做的完全一样,但imageNamed: 在同名的UIImage 实例之间共享内存。这是你浪费的记忆。
    • 这个想法是尝试手动创建CGImage,以查看UIImage 创建方法是否有问题。你是对的,这是浪费内存,但这不是这个答案的重点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多