我需要从facebook图形api获取100x100的方形图片。图片必须像标准方形"50x50"图片一样裁剪,但大小必须为 100x100。谢谢!
仅图形 API* 提供以下大小(使用类型参数指定图片大小):
-
正方形:50x50 像素
-
小:50 像素宽,高度可变
-
正常:100 像素宽,高度可变
-
大:宽约 200 像素,高度可变
如果您希望图像为 100x100,则必须检索"正常"大小并自己裁剪,例如,如果您使用的是 php,请检查 imagecopyresampled 函数
* 更新:
正如下面的评论所指出的,这个答案在 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)首先,您必须从网址下载图像:
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/<%= @problem.user.uid %>/pic...
请注意,您可以将尺寸更改为所需的任何尺寸(例如:50x50 或 500x500)。它应该从照片的中心裁剪和调整大小。它(出于某种原因)有时会大或小几个像素,但我认为这与原始照片的尺寸有关。耶,第一个答案是关于SO!
这是我使用上面的链接在 100 x 100 的愚蠢杯子。我会提供更多,但 SO 阻止发布多个链接,因为我是 n00b。
100 x 100:https://graph.facebook.com/8644397/picture?width=100&height=100