2013-02-10 2 views
1

Я пытаюсь открыть одну из своих картин на instagram, но каждый раз, когда я нажимаю кнопку действия (как вы можете видеть из моего кода), она показывает значок instagram и когда я нажимаю значок Instagram, сбой приложений. Что я делаю не так? Я застрял на этом некоторое время.Обмен изображениями с Instagram на iOS

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-02-10 13: 34: 46,206 Доли в Instagram [2197: 907] файл: // Localhost/вар/мобильная/Применения /0AABBF7B-F479-44E7-BA7F-B0FAA636F1CB/Documents/Image.ig

+0

Опубликовать фактический сбой? – Tim

ответ

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

Во-первых, убедитесь, что изображение не nil после imageNamed: вызова.

Затем, пожалуйста, imageData не nil после звонка UIImagePNGRepresentation.

Наконец, проверьте результат записи изображения в файл, прежде чем пытаться использовать его:

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

Возможно ваш файл не записывается, и поэтому пытаются разделить файл, который не делает существует причина вашего сбоя.

+0

ok Я сделал функцию bool и вот мой результат 2013-02-10 13: 34: 46.206 share to instagram [2197: 907] file: // localhost/var/mobile/Applications/0AABBF7B-F479-44E7-BA7F- B0FAA636F1CB/Документы/Image.ig –