简体   繁体   中英

Sharing Image on instagram using anyone sdk Or PHPhotolibrary?

I want to share the imageurl to instagram and i know very well it is possible by uidocumentinteraction controller.

But for my understanding there is no official sdk in instagram to sharing the image. So my question is without using uidocumentinteraction is it possible to share the image using sdk or PHPhotolibrary ?

Finally i found a solution.

UIImage *getImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest * assetReq = [PHAssetChangeRequest creationRequestForAssetFromImage:getImage];
    NSString* localId = [[assetReq placeholderForCreatedAsset] localIdentifier];
    NSString *escapedString = [localId stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
    NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@", escapedString]];

    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
        [[UIApplication sharedApplication] openURL:instagramURL];
    } else {
        NSLog(@"Instagram app not found.");
    }


} completionHandler:^(BOOL success, NSError *error) {
    if (!success){
        NSLog(@"%@",error);
    }
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM