Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

setting imageview first

TongoTongo Posts: 12Registered Users
Hello

I have this code



// Place image in ImageView

self.vImage.image = image;

tongo.time = 13;

// Upload Image to server

NSData *imageDataUpload = UIImageJPEGRepresentation(vImage.image, 90);
NSString *urlString = @\"http://website/upload.php\";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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=\\".png\\"\r\n\"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@\"Content-Type: application/octet-stream\r\n\r\n\"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageDataUpload]];
[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];

NSLog(returnString);

if (returnString) {
self.imageLabel.text = @\"Uploaded\";
}


What it does is, takes a picture -> places picture in the imageview -> uploads picture to server using the php file.

Everything is working fine however the only problem is that after it takes the picture, it will wait till its uploaded to the server before displaying the image in the imageview. How can I display it in the imageview before it begins uploading?
Post edited by Tongo on

Replies

  • gmarrogmarro Posts: 9New Users
    Tongo;440454 said:

    Everything is working fine however the only problem is that after it takes the picture, it will wait till its uploaded to the server before displaying the image in the imageview. How can I display it in the imageview before it begins uploading?
    Any change you make to the user interface is taken in account by the system as soon as your method returns and the control backs to the main loop on main thread. So, as you are uploading synchronously the file, nothing changes for the user interface until that upload finishes.

    The easiest way to solve that is to separate the uploading in a different method. For example:

    -(void) uploadFile:(NSString*) path ;

    Then, in your actual code, you write:

    [self performSelector:@selector(uploadFile:) withObject:path afterDelay:0.0];

    then the method ends, control returns to the main loop, the user interface does the modifications and immediately the new uploadFile: method is invoked.

    Gabriel
  • Duncan CDuncan C Posts: 8,031Tutorial Authors, Registered Users
    Tongo;440454 said:
    Hello

    I have this code



    // Place image in ImageView

    self.vImage.image = image;

    tongo.time = 13;

    // Upload Image to server

    NSData *imageDataUpload = UIImageJPEGRepresentation(vImage.image, 90);
    NSString *urlString = @\"http://website/upload.php\";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [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=\\".png\\"\r\n\"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@\"Content-Type: application/octet-stream\r\n\r\n\"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageDataUpload]];
    [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];

    NSLog(returnString);

    if (returnString) {
    self.imageLabel.text = @\"Uploaded\";
    }


    What it does is, takes a picture -> places picture in the imageview -> uploads picture to server using the php file.

    Everything is working fine however the only problem is that after it takes the picture, it will wait till its uploaded to the server before displaying the image in the imageview. How can I display it in the imageview before it begins uploading?

    You are sending your POST request synchronously. That freezes everything until the post is complete. UI changes like installing an image into an image view don't take effect until your code returns, which doesn't happen because of the synchronous POST.

    Rewrite your code to do the post asynchronously. You should use the NSURLConnection sendAsynchronousRequest:queue:completionHandler instead of using sendSynchronousRequest.

    That code might look like this:


    [NSURLConnection sendAsynchronousRequest: request
    queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    completionHandler: ^(NSURLResponse* response, NSData* returnData, NSError* error)
    {
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(returnString);
    if (returnString)
    {
    self.imageLabel.text = @\"Uploaded\";
    }
    [returnString release]; //Remove this line if you are using ARC
    }
    ];


    Disclaimer: I still struggle with the syntax for blocks that take parameters, so the syntax above may not be correct.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.