如何使用FTP直接从iOS上传图像到服务器目录

时间:2012-03-10 15:48:08

标签: ios objective-c iphone ftp objective-c++

我已经搜索了很多关于如何将图像从我的相机胶卷上传到服务器目录而我没有找到任何东西!所以我从Apple(SimpleFTPSample)下载了示例代码,我把这些代码带到了下面:

- (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];
    }
}

这些动作执行上传,但仅适用于项目文件夹中的图像。那么如何上传我从相机胶卷中选择的图像?

注意:我已经创建了一个UIImageView,并且正常显示来自相机胶卷的图像,但是如何上传UIImageView中显示的那些图像

2 个答案:

答案 0 :(得分:2)

下载这些library

如何使用它:

#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

您现在可以将myImageAsString发布到服务器..处理您的Web脚本中的myImageAsString数据。如果你想使用php,this可能会帮助你..

这就是我在网络服务器中管理图像的方式。

            <?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);
            } 

答案 1 :(得分:1)

由于您无法直接访问相册中的图像文件,因此您必须使用资产库来获取图像文件的NSData并直接上传该二进制缓冲区(将您的fileStream创建为[NSInputStream inputStreamWithData:...]

如何获取资产的NSData iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data

此线程中提到的另一种方法涉及实际重新压缩图像数据,可能会丢失质量/保真度,这在某些情况下可能至关重要。