简体   繁体   中英

Upload image using xcode (MySQL + PHP)

I'm trying to upload an image to a MySQL database using PHP in XCODE but I'm having some problems because it's not working. It's working fine when the user inserts a text, but when it comes to an image I can't make it.

I have read some tutorials and example but they are not working in my xcode project. Can you help me with this problem?

That's my code:

- (IBAction)uploadPhoto:(UIButton *)sender {

    NSLog(@"Photo enviada");
    /*
    NSString *strURL = [NSString stringWithFormat:@"http://example.com/upload.php?image=%@",_imageView.image];
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    NSLog(@"%@", strResult);
    */
    NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 90);
    NSString *urlString = @"http://autograpp.com/upload.php";

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}

But I'm getting an error on:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

ARC forbids explicit message send of autorelease.

Thanks in advance.

You are using - autorelease in an ARC-enabled project; this is neither necessary nor allowed. This code is probably a bit older and pre-dates ARC (automatic reference counting.)

If you replace

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

with

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

it should work.

In ARC, you do not have to retain/release objects any more; this is done automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM