1

Instagram で自分の写真の 1 つを開こうとしていますが、アクション ボタンを押すたびに (私のコードからわかるように)、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] file://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

まず、画像が通話 nil後にないことを確認してください。imageNamed:

次に、が通話後に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 に答える