簡體   English   中英

iOS應用程序下載flickr照片

[英]iOS app download flickr photos

我正在為一所學校開發一個應用程序。 他們將照片保存在flickr上。 他們希望在他們的應用中有一個照片庫頁面,可以從他們的flickr頁面中提取照片。 我想知道從flickr下載照片最簡單的方法是考慮:1。我不想上傳任何照片。 2.我不希望用戶必須登錄.3。我無法提供單個照片的網址,因為學校可能會比我更新應用程序更頻繁地更新他們的flickr。 我只是想讓應用程序在他們的專輯中提取所有照片。

非常感謝,盧克

試試這個代碼

-(void)getUserID{

    NSString *methodName = @"flickr.people.findByUsername";

    NSString *userName = @"<Your_Username>";

    NSString *urlString = [NSString stringWithFormat:@"%@?method=%@&api_key=%@",kFlickrAPIURL,methodName,kFlickrAPIKey];
urlString = [NSString stringWithFormat:@"%@&username=%@&format=%@&nojsoncallback=1", urlString,userName,kFlickrResponseFormat];

    NSURL *url = [NSURL URLWithString:urlString];

    userIDRequest = [[NSURLRequest alloc] initWithURL: url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:userIDRequest delegate:self];
    [connection release];
}

-(void)getUserPhotos:(NSString *)userID{

    NSString *methodName = @"flickr.people.getPhotos";

    NSString *urlString = [NSString stringWithFormat:@"%@?method=%@&key=%@",kFlickrAPIURL,methodName,kFlickrAPIKey];    

    urlString = [NSString stringWithFormat:@"%@&user_id=%@&format=%@&nojsoncallback=1",urlString,userID,kFlickrResponseFormat];

    NSURL *url = [NSURL URLWithString:urlString];

    imagesRequest = [[NSURLRequest alloc] initWithURL: url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:imagesRequest delegate:self];
    [connection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSError *error = nil;

    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    if (userIDRequest == connection.originalRequest)
    {
        NSString *userId = [[jsonDic objectForKey:@"user"] objectForKey:@"id"];
        if (userId != nil)
            [self getUserPhotos:userId];
    } else if (imagesRequest == connection.originalRequest) {

        NSArray *photos = [[jsonDic objectForKey:@"photos"] objectForKey:@"photo"];

        for (NSDictionary *photo in photos)
        {
            // Get title of the image
            NSString *title = [photo objectForKey:@"title"];

            // Build the URL to where the image is stored (see the Flickr API)
            // In the format http://farmX.static.flickr.com/server/id/secret
            // Notice the "_s" which requests a "small" image 75 x 75 pixels
            NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com", [photo objectForKey:@"farm"]];
            photoURLString = [NSString stringWithFormat:@"%@/%@/%@_%@_s.jpg", photoURLString, [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];

        }
     }

  }

下載ObjectiveFlickr ,示例文件夾中有一個示例“RandomPublicPhoto”

暫無
暫無

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

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