Instagram 照片到 UIImageView
Instagram photo to UIImageView
我可以在我的 UIImageView 上显示 Instagram 照片吗?
我一直在寻找媒体 ID 和其他选项,但找不到格式和显示此图片的方式,例如:
https://www.instagram.com/p/9W-K0wtq3v/
您可以使用 http://api.instagram.com/oembed?url= 直接 link 获取图像。之后,从 URL 下载图像并将其显示在 UIImageView
中非常简单。我已经编辑了我对此的回答,因为它无需集成 Instagram API 或解析网页以供 URL 归档。
将以下内容添加到视图控制器的方法中。我在评论中添加了解释。
- (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]
)
现在您已经设置了文件的 Web 请求部分。要使用这些服务,请将以下方法添加到您的 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
}
}];
}
这一切都没有白费。现在,你们都准备好了。您只需要一个 URL 到 instagram post,您只需调用以下一行即可下载您的图片:
[self getImageForPostURL:@"https://www.instagram.com/p/9W-K0wtq3v/"]; //Give your post url as parameter here
我认为:它将有两种方式来实现你的目标
*首先:解析 link 网络 Instagram。如果您查看源代码,您的 link 给出:您会发现直接 link 到图像:
https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-15/e35/10729433_781480525295113_239273684_n.jpg
所以你可以解析网页并找到:
<meta property="og:image" content="
直接 link.
您注册开发者 instagram 并学习如何使用 Instagram API 端点。
我可以在我的 UIImageView 上显示 Instagram 照片吗?
我一直在寻找媒体 ID 和其他选项,但找不到格式和显示此图片的方式,例如:
https://www.instagram.com/p/9W-K0wtq3v/
您可以使用 http://api.instagram.com/oembed?url= 直接 link 获取图像。之后,从 URL 下载图像并将其显示在 UIImageView
中非常简单。我已经编辑了我对此的回答,因为它无需集成 Instagram API 或解析网页以供 URL 归档。
将以下内容添加到视图控制器的方法中。我在评论中添加了解释。
- (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]
)
现在您已经设置了文件的 Web 请求部分。要使用这些服务,请将以下方法添加到您的 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
}
}];
}
这一切都没有白费。现在,你们都准备好了。您只需要一个 URL 到 instagram post,您只需调用以下一行即可下载您的图片:
[self getImageForPostURL:@"https://www.instagram.com/p/9W-K0wtq3v/"]; //Give your post url as parameter here
我认为:它将有两种方式来实现你的目标
*首先:解析 link 网络 Instagram。如果您查看源代码,您的 link 给出:您会发现直接 link 到图像:
https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-15/e35/10729433_781480525295113_239273684_n.jpg
所以你可以解析网页并找到:
<meta property="og:image" content="
直接 link.
您注册开发者 instagram 并学习如何使用 Instagram API 端点。