簡體   English   中英

如何使用NSURLSession保存Facebook個人資料圖片?

[英]How do I save a facebook profile picture using NSURLSession?

我正在創建一個簡單的facebook登錄應用程序,在我的viewdidload()方法中,我具有權限,並且正在獲取名稱,用戶名,電子郵件ID和個人資料圖片鏈接,但是我想做的就是嘗試下載該鏈接(URL)並在本地保存/下載。

剛開始使用X代碼,將不勝感激。

> - (void)viewDidLoad {
>     
>      [super viewDidLoad];    
>     FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];    loginButton.center = self.view.center;    [loginButton
> setDelegate:self];
>     loginButton.loginBehavior=FBSDKLoginBehaviorWeb;
>         [self.view addSubview:loginButton];
>     loginButton.readPermissions = @[@"public_profile",@"email",@"user_friends",@"user_about_me",@"user_friends",@"user_birthday",@"user_hometown"];
>     loginButton.publishPermissions=@[@"publish_actions"];
>     FBSDKLoginManagerLoginResult *result;
>     
>     
> 
>     {
>         if ([result.grantedPermissions containsObject:@"email"]) {
>             NSLog(@"%@",result);
>         }
>     }
> 
>     NSMutableDictionary* param = [NSMutableDictionary dictionary];
>     [param setValue:@"id,name,email,picture.width(100).height(100),bio"
> forKey:@"fields"];
> 
> 
>     NSURL *profilePictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/user/picture?type=large"]];
>                                                       NSURLRequest *profilePictureURLRequest = [NSURLRequest requestWithURL:profilePictureURL
> cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
> // Facebook profile picture cache policy: Expires in 2 weeks
>     [NSURLSession sessionWithConfiguration:profilePictureURLRequest delegate:self delegateQueue:nil];

使用您的NSURLRequest創建一個NSURLSessionDataTask任務:

NSURL *profilePictureURL = ...;
NSURLRequest *profilePictureURLRequest = ...;

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:profilePictureURLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    // This block is called when the request completes / fails.

    if ((!error) && (data.length > 0)) {

        // Write to local URL on background thread.
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

            NSURL *someFileUrl = ...;

            [data writeToUrl:someFileUrl atomically:YES];

        });
    }
}];

// Send the request...
[task resume];

嘗試使用此代碼,它將圖像保存在應用程序文檔目錄中。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",[result objectForKey:@"id"]]]];
                    [imageData writeToFile:[NSString stringWithFormat:@"%@/profilePic.jpg",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]] atomically:YES];
                })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM