ASIHttpRequest上传图片

时间:2012-07-07 15:39:24

标签: image cocoa upload asihttprequest yfrog

嘿,我有一个带有以下代码的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

那我怎样才能正确上传图片呢?

非常感谢。

1 个答案:

答案 0 :(得分: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];
}