1

Before give duplicate mark read my whole question:

I know might be there is many question related to my title but issue is different.

I am using Mac OS X 10.9.3 and Xcode 5.1.1.

In my app I just want to share image, URL link and some description on Facebook and Twitter so I used SLComposeViewController and my code is below.

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

Method body:

-(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.
}

After execute above code, SLComposeViewController will be open and in my console

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

display only with Facebook and get exactly same problem Look at this question. I am sure my image is not nil, I was checked it.

Above question said -

This bug seems to be fixed in new version of iOS (6.0.1) At least I have all working well since my last updgrade.

But I am testing on iOS 6.1 still I am getting same issue and in iOS 7.0 and 7.1 the error message in console (<Error>: CGImageCreate: invalid image size: 0 x 0.) Not display but I cam not able to go to on setting App in both Facebook and Twitter.
What is problem ? where I am wrong? Is this apple Bug ??
Please give your suggestion.

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Can you give it just a try : declare `SLComposeViewController` as property and see. – Maulik Jun 02 '14 at 14:01
  • @Maulik - Tried it, Not working for me :( – iPatel Jun 02 '14 at 14:07
  • I would like to get your code :) – Maulik Jun 02 '14 at 14:16
  • Just to be sure of how you responded to the comment above - you did make "SLComposeViewController *shareKit" either a strong ivar or a strong property? Usually bad form to not keep a strong reference to an object like this. Also, instead of one line "[shareKit addImage:[UIImage imageNamed:@"boston-shopping.jpg"]];" use a UIImage variable, set it to the image, then assert(image) or at least log it and see what you get. – David H Jun 02 '14 at 14:50
  • @DavidH - I have tried with declare object of `SLComposeViewController` with `@property` but it does not work for me and also i tried with use of UIImage *image = [UImage... its not working :( – iPatel Jun 02 '14 at 15:24
  • 1
    So try commenting out the line where you add the image - any change? Try putting a breakpoint on CGImageCreate() and see what the stack says. Failing that, build a small demo app that has the problem, put it on Dropbox, and offer a bounty for the proper fix. – David H Jun 02 '14 at 16:59
  • 1
    a minimal project to reproduce the issue would help... – sergio Jun 06 '14 at 09:58
  • @iPatel Same issue aries in my project as my knowledge it's iOS Bug – Nimit Parekh Jun 11 '14 at 11:36

2 Answers2

1

Take a look at this stack trace after I called [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

I tried to run your code in my sample project and I found these :

  • The error message was printed after [self presentViewController:shareKit animated:YES completion:nil]; and before the modal shareKit view appeared.
  • The error message was printed from CGPostError in CoreGraphics.
  • I think this error occurred because of -[UISnapshotView captureSnapshotRect:fromView:withSnapshotType:].
  • I tried to comment out this line of yours, [shareKit addImage:[UIImage imageNamed:@"boston-shopping.jpg"]];, and I still got the error message. So, the error was not because of your code.

In my opinion, I think the error message you got was because of UISnapshotView was trying to take a snapshot of the current screen, but there was nothing there or there was some bugs in the method. So, it passed the wrong width = 0 & height = 0 arguments to CGImageCreate that caused the error.

So, rest assured, this is not your bug. Just ignore it.

3329
  • 1,411
  • 13
  • 17
-1

Try and create the CGImageRef object yourself. You can replace the line:

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

with this:

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);
Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • I tried your code, its not working for me.. Same issue .. its might be bug of Apple still not solve.. :( – iPatel Jun 07 '14 at 04:00
  • Every `UIImage` has a `CGImage` inside. This way you are just wasting data. – Sulthan Jun 10 '14 at 19:50
  • @Sulthan I don't understand your issue with this code. All I've done here is offer a way to manually create the `UIImage` object via a `CGImage`. There's no wastage here. – Guy Kogus Jun 11 '14 at 09:40
  • @GuyKogus How is that better than `+[UIImage imageNamed:]`? You do absolutely the same but `imageNamed:` shares memory between `UIImage` instances with the same name. There's your wasted memory. – Sulthan Jun 11 '14 at 09:42
  • The idea was to try and create the `CGImage` manually to see if the `UIImage` creation method was doing something wrong. You're right that it's wasted memory but that wasn't the point of this answer. – Guy Kogus Jun 11 '14 at 10:35