0

嘿,我有一个带有以下代码的 mac 应用程序:

 NSURL *url;
        url = [NSURL URLWithString:@"http://yfrog.com/api/xauth_upload"];

        ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];

        [request setDelegate:self];
        [request setUseCookiePersistence:NO];
        [request setUploadProgressDelegate:self];
        [request showAccurateProgress]; 
        [request signRequestWithClientIdentifier:@"w" secret:@"x" tokenIdentifier:@"y" secret:@"z" usingMethod:ASIOAuthHMAC_SHA1SignatureMethod];
        [request setPostValue:@"a" forKey:@"key"];
    NSData *imageData = [[NSData alloc]initWithContentsOfFile:[draggedFilenames objectAtIndex:0]];
    [request setPostValue:imageData forKey:@"media"];

        [request startAsynchronous]; 

但问题是我总是从 yfrog 服务器得到这个响应:

所以图片上传有问题。在 iPhone 上它是这样工作的:

[request setData:UIImageJPEGRepresentation(picture, 0.8) withFileName:@"filename.jpg" andContentType:@"image/jpeg" forKey:@"media"];

但这不起作用 cocoa(mac) 因为没有像 UIIMageJPEGRepresentation 这样的方法。身份验证也有效,这不是问题。如果您向您询问此方法 signRequestWithClientIdentifier 它来自此处的 asihttprequest+oauth 包装器: https ://github.com/keybuk/asi-http-request-oauth

那么如何正确上传图片呢?

非常感谢。

4

1 回答 1

2
NSData* NSImageJPEGRepresentation(NSImage* image, CGFloat compressionQuality)
{
    if(nil == image)return nil;
    NSDictionary *dict;
    dict = [NSDictionary dictionaryWithObjectsAndKeys:
            [NSNumber numberWithFloat:compressionQuality],
            NSImageCompressionFactor,
            nil];

    NSBitmapImageRep* imageRep;
    imageRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
    return [imageRep representationUsingType:NSJPEGFileType 
                                  properties:dict];
}
于 2012-07-07T15:56:08.303 回答