1

我试图在 instagram 上打开我的一张照片,但每次我按下操作按钮(如您从我的代码中看到的那样)时,它都会显示 instagram 图标,当我按下 Instagram 图标时,应用程序就会崩溃。我究竟做错了什么?我已经坚持了一段时间。

interface  ViewController : UIViewController <UIDocumentInteractionControllerDelegate>{
IBOutlet UIImageView *onlyImageVIew;    
IBOutlet UIImageView *myImageView;

  }
@property (nonatomic, retain) UIDocumentInteractionController *docController; 
  -(IBAction)actionButton:(id)sender;

@end



 -(IBAction)actionButton:(id)sender {

NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"Image.ig"];

    UIImage *image = [UIImage imageNamed:@"01.png"];

    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:YES];
    NSURL *imageUrl = [NSURL fileURLWithPath:savedImagePath];
    NSLog(@"%@",imageUrl);
    UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
    docController.delegate = self;
    docController.UTI = @"com.instagram.photo";
    docController.URL = imageUrl;
    //[docController setURL:imageUrl];
    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];



}
}

这是我实现 bool 函数时的崩溃

2013-02-10 13:34:46.206 分享到 instagram[2197:907] 文件://localhost/var/mobile/Applications/0AABBF7B-F479-44E7-BA7F-B0FAA636F1CB/Documents/Image.ig

4

2 回答 2

1

希望下面的代码对你有用。

-(void)ShareInstagram
{
UIImagePickerController *imgpicker=[[UIImagePickerController alloc] init];
imgpicker.delegate=self;
[self storeimage];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{

    CGRect rect = CGRectMake(0 ,0 , 612, 612);
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/15717.ig"];

    NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
    dic.UTI = @"com.instagram.photo";
    dic.delegate=self;
    dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
    dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
    dic.delegate=self;
    [dic presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];
    //  [[UIApplication sharedApplication] openURL:instagramURL];
}
else
{
    //   NSLog(@"instagramImageShare");
    UIAlertView *errorToShare = [[UIAlertView alloc] initWithTitle:@"Instagram unavailable " message:@"You need to install Instagram in your device in order to share this image" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    errorToShare.tag=3010;
    [errorToShare show];
}
}


- (void) storeimage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"15717.ig"];
UIImage *NewImg=[self resizedImage:imageCapture :CGRectMake(0, 0, 612, 612) ];
NSData *imageData = UIImagePNGRepresentation(NewImg);
[imageData writeToFile:savedImagePath atomically:NO];
}

-(UIImage*) resizedImage:(UIImage *)inImage: (CGRect) thumbRect
{
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;

// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
                                            NULL,
                                            thumbRect.size.width,       // width
                                            thumbRect.size.height,      // height
                                            CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                                            4 * thumbRect.size.width,   // rowbytes
                                            CGImageGetColorSpace(imageRef),
                                            alphaInfo
                                            );

// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);

// Get an image from the context and a UIImage
CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
UIImage*    result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);   // ok if NULL
CGImageRelease(ref);

return result;
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate
{


    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    interactionController.delegate = self;

    return interactionController;
}

- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
{

}

- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action
{
//    NSLog(@"5dsklfjkljas");
    return YES;
}

- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action
 { 
//    NSLog(@"dsfa");
     return YES;
 }

 - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
 {
     //    NSLog(@"fsafasd;");
 }

这段代码对我有用。祝你好运。

于 2013-02-11T06:10:07.873 回答
0

首先,确保您的图像不在通话nilimageNamed:

然后,确保imageData不在通话nilUIImagePNGRepresentation

最后,在尝试使用之前检查将图像写入文件的结果:

BOOL writingResult = [imageData writeToFile:savedImagePath atomically:YES];
if(writingResult == NO)
{
    NSLog(@"Failed to write to %@",savedImagePath);
    return;
}

您的文件可能没有被写入,因此尝试共享一个不存在的文件会导致您的崩溃。

于 2013-02-10T19:21:58.330 回答