2012-03-10 2 views
1

내 카메라 롤에서 서버 디렉토리로 이미지를 업로드하는 방법을 많이 찾았지만 아무 것도 찾지 못했습니다! 단지 내가 선택한 이미지를 어떻게 업로드 할 수 있습니다 folder.So 내 프로젝트 안에있는 이미지어떻게하면 FTP를 사용하여 iOS를 직접 서버 디렉토리에 업로드 할 수 있습니까?

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

그 조치가 업로드를 수행하지만, 애플 (SimpleFTPSample), 그리고 난에서 샘플 코드를 넣고 다음 코드를했다 내 카메라 롤에서?

주의 사항 : 나는 이미있는 UIImageView를 생성하고 정상적으로, 카메라 롤에서 이미지를 표시 만들었지 만, 어떻게 UIImageView에

+0

NSString을 제공하고 문자열을 서버에 게시하고 이미지를 생성하기 위해 웹 스크립트를 통해 문자열을 처리하는 방법은 base64로 이미지를 인코딩하는 방법은 무엇입니까? 그런 다음 이미지를 서버에 저장하십시오. – janusbalatbat

+0

샘플 코드 또는 자습서를 제공 할 수 있습니까? 여기에서 검색했지만 찾지 못했습니다! 미안하지만 초보자라는 것을 잊어 버렸습니다. – Mateus

답변

2

다운로드이 library에 표시하는 이미지를 업로드 할 수 있습니다했습니다.

는 사용 방법 : 당신은 지금 당신의 웹 스크립트 프로세스 myImageAsString 데이터 .. 서버에 myImageAsString을 게시 할 수 있습니다

#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 

. 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); 
      } 
+0

그래서 이미 라이브러리를 다운로드하고 가져 왔습니다. 이제 HTTP 요청을 만들고 NSString을이 데이터와 함께 내 서버의 PHP 파일로 보내야합니다. 그리고 이미지가 내 디렉토리에 업로드 된 후에야합니까? – Mateus

+0

당신은 그것에 작업해야 :) 나는 내 대답을 업데이 트합니다 – janusbalatbat

+0

미안하지만, 나에게 분명히, 나는 단지 오류가 추락 시뮬레이터를 작동하도록 넣어려고! – Mateus

1

직접 당신은 ([NSInputStream inputStreamWithData 같은 파일 스트림을 생성 할 이미지 파일의있는 NSData을 잡고 직접 바이너리 버퍼를 업로드 할 자산 라이브러리를 사용할 수 있습니다 ... ]

자산 iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data

실제로 특정 상황에서 중요 할 수있는 품질/충실도의 잠재적 인 손실과 이미지 데이터를 다시 압축 포함이 스레드에서 언급 된 다른 방법에있는 NSData를 얻을하는 방법.

,
관련 문제