gpt4 book ai didi

iphone - 在 iOS 上将图像分享到 Instagram

转载 作者:行者123 更新时间:2023-11-29 13:19:13 31 4
gpt4 key购买 nike

我试图在 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] file://localhost/var/mobile/Applications/0AABBF7B-F479-44E7-BA7F-B0FAA636F1CB/Documents/Image.ig

最佳答案

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

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

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

关于iphone - 在 iOS 上将图像分享到 Instagram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14801359/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com