【发布时间】:2012-05-17 15:30:57
【问题描述】:
我需要从 facebook graph api 获取 100x100 的正方形图片。图片必须像标准方形“50x50”图片一样裁剪,但尺寸必须为100x100。谢谢!
【问题讨论】:
我需要从 facebook graph api 获取 100x100 的正方形图片。图片必须像标准方形“50x50”图片一样裁剪,但尺寸必须为100x100。谢谢!
【问题讨论】:
Graph API 仅* 提供以下尺寸(使用 type 参数指定图片尺寸):
正方形:50x50像素
小:50 像素宽,可变高度
正常:100 像素宽,可变高度
大:大约 200 像素宽,可变高度
如果您希望图像为 100x100,则必须检索“正常”尺寸并自行裁剪,例如如果您使用的是 php,请检查 imagecopyresampled 函数
* 更新:
正如下面的 cmets 所指出的,这个答案在 2012 年 5 月是正确的,但现在您还可以选择使用 graph.facebook.com/UID 获得不同的尺寸/picture?width=N&height=N,如 Jeremy 最近的回答中所述。
【讨论】:
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage *)crop:(CGRect)rect andIm:(UIImage*) im {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale>1.0) {
rect = CGRectMake(rect.origin.x*scale , rect.origin.y*scale, rect.size.width*scale, rect.size.height*scale);
}
CGImageRef imageRef = CGImageCreateWithImageInRect([im CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return result;
}
-(UIImage*)squareLargeFacebookProfilePhoto:(UIImage*) image{
float startPosX=0.0;
float startPosY=0.0;
float newsizeW;
float newsizeH;
if (image.size.height>=image.size.width){
newsizeW=200;
float diff=200-image.size.width;
newsizeH=image.size.height+diff/image.size.width*image.size.height;
if (newsizeH>200){
startPosY=(newsizeH-200.0)/8.0;
}
}
else
{
newsizeH=200;
float diff=200-image.size.height;
newsizeW=image.size.width+diff/image.size.height*image.size.width;
if (newsizeW>200){
startPosX=(newsizeW-200.0)/2.0;
}
}
UIImage *imresized=[self imageWithImage:image scaledToSize:CGSizeMake(newsizeW, newsizeH)];
return [self crop:CGRectMake(startPosX, startPosY, 200, 200) andIm:imresized];
}
网址https://graph.facebook.com/user_id/picture?type=square 给了我们一个小 个人资料图片 (50x50)
1)首先你要从url下载图片:
https://graph.facebook.com/user_id/picture?type=large
例如加载图片
NSURL * imageURL = [NSURL URLWithString:https://graph.facebook.com/user_id/picture?type=large];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
2)然后只需调用将返回大小为 (200x200) 的大型 Facebook 个人资料图片的函数:
UIImage *LargeProfileImage=[self squareLargeFacebookProfilePhoto:image];
注意:代码是使用 ARC 内存管理编写的
【讨论】:
你也可以试试这个:
https://graph.facebook.com/INSERTUIDHERE/picture?width=100&height=100
不要忘记将“INSERTUIDHERE”替换为您尝试为其获取图像的用户的 UID。嵌入式红宝石在这里工作得很好。例如:...ook.com//pic...
请注意,您可以将尺寸更改为任何您想要的尺寸(例如:50x50 或 500x500)。它应该从照片的中心裁剪和调整大小。它(出于某种原因)有时会大或小几个像素,但我认为这与原始照片的尺寸有关。耶,第一个答案!
这是我使用上面链接的 100 x 100 的愚蠢杯子。我会提供更多,但由于我是 n00b,所以阻止发布多个链接。
100 x 100:https://graph.facebook.com/8644397/picture?width=100&height=100
【讨论】: