How can i upload an image to server directory dire

2019-08-13 20:40发布

问题:

I've searching a lot on how to upload images from my camera roll to a server directory and i didn't found anything!So i've downloaded the sample code from Apple(SimpleFTPSample),and i took these code bellow:

- (void)_startSend:(NSString *)filePath
{
    BOOL                    success;
    NSURL *                 url;
    CFWriteStreamRef        ftpStream;

    assert(filePath != nil);
    assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
    assert( [filePath.pathExtension isEqual:@"png"] || [filePath.pathExtension isEqual:@"jpg"] );

    assert(self.networkStream == nil);      // don't tap send twice in a row!
    assert(self.fileStream == nil);         // ditto

    // First get and check the URL.

    url = [[AppDelegate sharedAppDelegate] smartURLForString:self.urlText.text];
    success = (url != nil);

    if (success) {
        // Add the last part of the file name to the end of the URL to form the final 
        // URL that we're going to put to.

        url = [NSMakeCollectable(
            CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url, (CFStringRef) [filePath lastPathComponent], false)
        ) autorelease];
        success = (url != nil);
    }

    // If the URL is bogus, let the user know.  Otherwise kick off the connection.

    if ( ! success) {
        self.statusLabel.text = @"Invalid URL";
    } else {

        // Open a stream for the file we're going to send.  We do not open this stream; 
        // NSURLConnection will do it for us.

        self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
        assert(self.fileStream != nil);

        [self.fileStream open];

        // Open a CFFTPStream for the URL.

        ftpStream = CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url);
        assert(ftpStream != NULL);

        self.networkStream = (NSOutputStream *) ftpStream;

        if (self.usernameText.text.length != 0) {
            #pragma unused (success) //Adding this to appease the static analyzer.
            success = [self.networkStream setProperty:self.usernameText.text forKey:(id)kCFStreamPropertyFTPUserName];
            assert(success);
            success = [self.networkStream setProperty:self.passwordText.text forKey:(id)kCFStreamPropertyFTPPassword];
            assert(success);
        }

        self.networkStream.delegate = self;
        [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [self.networkStream open];

        // Have to release ftpStream to balance out the create.  self.networkStream 
        // has retained this for our persistent use.

        CFRelease(ftpStream);

        // Tell the UI we're sending.

        [self _sendDidStart];
    }
}

Those action perform the upload,but just for images which are inside my project folder.So how can upload an image that i've selected from my camera roll?

NOTICE:I've already create an UIImageView and made it display an image from camera roll,as normal,but how can upload those image which is displayed in the UIImageView

回答1:

download these library .

how to use it:

#import "NSData+Base64.h"
@implementation.....
-(void)encodeImageAndSendToServer
{
//prepare the image
NSData *imageData = UIImageJPEGRepresentation(myImageView.image, 1.0);
myImageView.image = [UIImage imageWithData:imageData];    
NSString *myImageAsString = [imageData base64EncodedString];
}
@end

you can now post myImageAsString to server.. process the myImageAsString data in your web script. if you want to use php,this might help you..

This is how i manage the image in my web server.

            <?php
            $imagestring = "your posted NSString from iphone";
            $file_name = 'myImage.jpg';
            $img = imagecreatefromstring(base64_decode($imagestring));
           if($img != false)
            {
             imagejpeg($img, '../images/comprofiler/gallery/'.$file_name);
            } 


回答2:

Since you can't access image file in your albums directly you have to use Assets Library to grab NSData of the image file and upload that binary buffer directly (create your fileStream as [NSInputStream inputStreamWithData: ...]

How to get NSData of the asset iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data

The other method mentioned in this thread involves actually recompressing image data with potential loss of quality/fidelity which might be crucial in certain situations.