【问题标题】:Instagram photo to UIImageViewInstagram 照片到 UIImageView
【发布时间】:2016-08-16 19:54:07
【问题描述】:

我可以在我的 UIImageView 上显示 Instagram 照片吗?

我正在寻找媒体 ID 和其他选项,但我找不到显示此图像的格式和方式,例如:

https://www.instagram.com/p/9W-K0wtq3v/

【问题讨论】:

  • NSURL、NSData 和 UIImageView。三个字。

标签: ios objective-c instagram


【解决方案1】:

您可以使用 http://api.instagram.com/oembed?url= 获得图片的直接链接。之后,从该 URL 下载图像并将其显示在 UIImageView 中非常简单。我已经编辑了对此的答案,因为它无需集成 Instagram API 或解析网页以获取文件的 URL。

将以下内容添加到视图控制器的方法中。我在 cmets 中添加了解释。

- (void)getDirectURLToLink:(NSString *)urlStr completionBlock:(void (^)(BOOL succeeded, NSString *imageLink))completionBlock
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if ( !error )
                               {
                                   //Convert data to dictionary as it is JSON. You can view json response for your url at http://paste.ubuntu.com/14437288/
                                    NSError *error1;
                                   NSMutableDictionary * innerJson = [NSJSONSerialization
                                                                      JSONObjectWithData:data options:kNilOptions error:&error1
                                                                      ];
                                   //Send the direct url to block
                                   completionBlock(YES,[innerJson objectForKey:@"thumbnail_url"]);
                               } else{
                                   //request failed
                                   completionBlock(NO,nil);
                               }
                           }];
}

- (void)downloadImageWithURL:(NSString *)urlStr completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if ( !error )
                               {
                                   //cnvert data to uiimage
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   completionBlock(YES,image);
                               } else{
                                   //download failed
                                   completionBlock(NO,nil);
                               }
                           }];
}

(但由于 sendAsynchronusRequest 在 iOS 9.0 中已弃用,您应该使用 [NSURLSession dataTaskWithURL]

现在您已经设置了文件的网络请求部分。要使用这些服务,请将以下方法添加到您的 Viewcontroller:

-(void) getImageForPostURL: (NSString *)postURL
{
    NSString *baseURL = @"http://api.instagram.com/oembed?url=";

    NSString *directLinkRequestURL = [NSString stringWithFormat:@"%@%@",baseURL,postURL];
    //Request Direct URL to file from your post url
    [self getDirectURLToLink:directLinkRequestURL completionBlock:^(BOOL succeeded, NSString *imgDirectURL) {
        if (succeeded) {
            //Direct link retrieved
            //Get image
            [self downloadImageWithURL:imgDirectURL completionBlock:^(BOOL succeeded, UIImage *image) {
                if (succeeded) {
                    // change the image where you want, it has been downloaded
                    _imgView.image = image;

                }

            }];

        }
        else
        {
           //Error
            //Link could not be retrieved

        }


    }];

}

所有这些工作都不是徒劳的。现在,你们都准备好了。您所需要的只是一个用于 instagram 帖子的 URL,您只需调用这一行即可下载您的图片:

[self getImageForPostURL:@"https://www.instagram.com/p/9W-K0wtq3v/"]; //Give your post url as parameter here

【讨论】:

    【解决方案2】:

    我认为:实现目标有两种方式

    *首先:解析链接网络 Instagram。如果您查看您的链接提供的源代码:您会找到图片的直接链接:

    https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-15/e35/10729433_781480525295113_239273684_n.jpg
    

    所以你可以解析网页并找到:

    <meta property="og:image" content="
    

    直接链接。

    您注册开发者 instagram 并学习如何使用 Instagram API 端点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多