2013-02-10 180 views
1

我想在Instagram上打開我的一張照片,但每次我按下動作按鈕時(正如您可以從我的代碼中看到的那樣)它會顯示instagram圖標,當我將Instagram圖標應用程序崩潰。我究竟做錯了什麼?我一直堅持這一點。在iOS上分享圖像到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]; 



} 
} 

這裏是碰撞時予實現的布爾函數

2013年2月10日13:34:46.206份額的Instagram [2197:907]的文件://本地主機的/ var /移動/應用/0AABBF7B-F479-44E7-BA7F-B0FAA636F1CB/Documents/Image.ig

+0

發佈實際崩潰? – Tim 2013-02-10 19:22:15

回答

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;"); 
} 

此代碼適用於我。 好運。

0

首先,在imageNamed:之後確保您的圖像不是nil

然後,確保imageData不是nilUIImagePNGRepresentation調用。

最後,嘗試使用它之前檢查圖像寫入文件的結果:

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

這是可能你的文件不被寫入,所以想分享一點不文件存在導致你的崩潰。

+0

好的我做了布爾函數,這裏是我的結果2013-02-10 13:34:46分享到instagram [2197:907] file:// localhost/var/mobile/Applications/0AABBF7B-F479-44E7-BA7F- B0FAA636F1CB /文檔/ Image.ig – 2013-02-10 19:35:33

相關問題