2017-07-31 35 views
0

イメージの選択のために、自分のプロジェクトでカスタムの複数イメージ選択サードパーティフォルダを使用する必要があり、これらのイメージは配列に入っています。選択はこのCreolePhotoSelection.IOSのサーバーにイメージの配列をアップロード

あるカスタムイメージのためのリンクは今、私はサービスに画像の配列をアップロードしようとしているが、それは私に、このエラーを与えている:

-[__NSDictionaryM CGImage]: unrecognized selector sent to instance 0x60800024dce0 2017-07-31 12:41:46.777 PK.House[7795:1224040] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM CGImage]: unrecognized selector sent to instance 0x60800024dce0'

画像をされてアップロードするための私のコード、

-(void)submit 
    { 

     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"yyyyMMddHHmmss"]; 
     NSDate *now = [[NSDate alloc] init]; 
     NSString *theDate = [dateFormat stringFromDate:now]; 
     NSLog(@"\n" 
       "theDate: |%@| ", theDate); 

     UIImage *image = [self.arrImages objectAtIndex:imagecount]; 
     imagecount++; 


     NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.7f)]; 
     NSLog(@"Data is %@",imageData); 

     NSString *baseurl = @"My URL"; 

     NSURL *dataURL = [NSURL URLWithString:baseurl]; 

     NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 

     [dataRqst setHTTPMethod:@"POST"]; 

     NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
     NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 

     [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 

     NSMutableData *postBody = [NSMutableData data]; 


     [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[NSString stringWithFormat:@"%@.png",theDate]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:imageData]; 
     [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

     [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [dataRqst setHTTPBody:postBody]; 

     NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init]; 
     NSLog(@"Response is %@",response); 
     NSError* error = nil ; 
     NSLog(@"Error is %@",error); 
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:dataRqst delegate:self]; 
     NSLog(@"Connection is %@",conn); 
    } 

    #pragma mark NSURLConnection Delegate Methods 

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

     self.receiveData = [[NSMutableData alloc] init]; 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

     [self.receiveData appendData:data]; 


     NSError* error; 

     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 

                  options:kNilOptions 

                   error:&error]; 
     NSLog(@"json..%@",json); 

     if (imagecount <[self.arrImages count]) { 

      [self submit]; 
     } 

     else 
    { 

      UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil 
                  message:@"Data uploaded successfully" 
                  delegate:nil 

                cancelButtonTitle:nil 

               otherButtonTitles:nil, nil] ; 
      [alert show]; 
     } 

    } 

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection 
         willCacheResponse:(NSCachedURLResponse*)cachedResponse { 

     return nil; 
    } 

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    } 

    -(void)connection:(NSURLConnection *)connection didSendBodyData: 

    (NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten 

    totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
    { 

    } 

マイセレクション画像コードはこれです:

- (IBAction)Select:(id)sender 
    { 
    CreolePhotoSelection *objCreolePhotoSelection= [[CreolePhotoSelection alloc] initWithNibName:@"CreolePhotoSelection" bundle:nil]; 
    objCreolePhotoSelection.strTitle = @"Images"; 
    objCreolePhotoSelection.delegate = self; // It will help to retrive all selected photos from CreolePhotoSelection 
    objCreolePhotoSelection.arySelectedPhoto = _arrImages; // You need to pass same array which is holding selected image 
    objCreolePhotoSelection.maxCount = 50; // Eg. 5, 10 anythings... 

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:objCreolePhotoSelection]; 
    [self.navigationController presentViewController:navController animated:YES completion:nil]; 

} 

と機能は以下のとおりです。

-(void)getSelectedPhoto:(NSMutableArray *)aryPhoto 

{ 
    // //Initlize array 

    // NSArray *aryAlImage = [aryPhoto valueForKeyPath:@"mainImage"]; 

    // 

    // NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:aryAlImage]; 

    _arrImages = nil; 

    _arrImages = [NSMutableArray new]; 

    _arrImages = [aryPhoto mutableCopy]; //mainImage, Asset, selected 

    [self setImageToScrollview:_arrImages]; 

} 

#pragma mark - Display Image to ScrollView 
-(void)setImageToScrollview:(NSMutableArray *)arrSetImage 

{ 
    // Remove all images from ScrollView and then add new one 

    for(id sender in _scrImageDisplay.subviews) 

     [sender removeFromSuperview]; 

    _scrImageDisplay.pagingEnabled = FALSE; 

    CGFloat height_width = CGRectGetHeight(_scrImageDisplay.frame); 

    NSInteger numberOfImages = arrSetImage.count; 


    NSLog(@"Size of array is %ld",(long)numberOfImages); 

    CGFloat xOrigin = 0; 

    for (int i = 0 ; i < numberOfImages ; i++) 

    { 
     xOrigin += 3; 

     CGRect rect = CGRectMake(xOrigin, 0,height_width,height_width); 

     UIImageView *Arrimage = [[UIImageView alloc] initWithFrame:rect]; 

     Arrimage.image = [[arrSetImage objectAtIndex:i] valueForKey:@"mainImage"]; 

     NSLog(@"Image data is %@",Arrimage); 

     Arrimage.contentMode = UIViewContentModeScaleAspectFill; 

     Arrimage.clipsToBounds = YES; 

     [_scrImageDisplay addSubview:Arrimage]; 

     xOrigin += height_width; 

    } 

    _scrImageDisplay.contentSize = CGSizeMake(xOrigin, height_width); 
} 
+0

「UIImage * image = [self.arrImages objectAtIndex:imagecount];」という行が問題を引き起こしているようです。それが事実であることを確認できますか? yesの場合、 '[self.arrImages objectAtIndex:imagecount];'は 'NSDictionary'であり、' UIImage'オブジェクトではありません。 '[self.arrImages objectAtIndex:imagecount];'を記録できますか? – Larme

+0

実際に私はこの行が問題を引き起こしているとも考えています。arrImagesにはすべての画像が含まれています。 @Larme –

+0

ログ '[self.arrImages objectAtIndex:imagecount];'、それは 'NSDictionary'です。私はあなたが 'self.arrImages'にどのように住んでいるのかわかりませんが、それは問題であるはずです。 – Larme

答えて

1

あなたは画像のアップロードタスクを実行するためにNSURLSessionを使用することができます。ここにコードがあります。

NSDictionary *headers = @{ @"content-type": @"application/json", 
          @"cache-control": @"no-cache", 
          }; 
NSDictionary *parameters = @[ @{ @"image": @“BASE_64_STRING”, @"proeprty_id": @"1", @"name": @"asd.png" } ]; 

NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:YOUR_SERIVCE_URL] 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:10.0]; 
[request setHTTPMethod:@"POST"]; 
[request setAllHTTPHeaderFields:headers]; 
[request setHTTPBody:postData]; 

NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request 
              completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
               if (error) { 
                NSLog(@"%@", error); 
               } else { 
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
                NSLog(@"%@", httpResponse); 
               } 
              }]; 
[dataTask resume]; 
+0

変数にイメージの次の名前を格納し、それを名前のパラメータに渡したいのですが? –

+0

あなたはパラメータDicでサーバに送ることができるように多くのパラを置くことができます。 – Nirmalsinh

+0

ありがとうございました。 :) @Nirmalsinh –

0
-(void)submit { 
     for (int i=0; i<_arrImages.count; i++) 
     { 
     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"yyyyMMddHHmmss"]; 
     NSDate *now = [[NSDate alloc] init]; 
     NSString *theDate = [dateFormat stringFromDate:now]; 
     NSLog(@"\n""theDate: |%@| ", theDate); 
     UIImage *image = [self.arrImages objectAtIndex:i]; 
     NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.7f)]; 
     NSLog(@"Data is %@",imageData); 
     NSString *baseurl = @"My URL"; 
     NSURL *dataURL = [NSURL URLWithString:baseurl]; 
     NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 
     [dataRqst setHTTPMethod:@"POST"]; 
     NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
     NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 
     [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 
     NSMutableData *postBody = [NSMutableData data]; 
     [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[NSString stringWithFormat:@"%@.png",theDate]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:imageData]; 
     [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [dataRqst setHTTPBody:postBody]; 
     NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init]; 
     NSLog(@"Response is %@",response); 
     NSError* error = nil ; 
     NSLog(@"Error is %@",error); 
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:dataRqst delegate:self]; 
     NSLog(@"Connection is %@",conn); 
     } 
    } 
+0

urによって、このエラーが返されます。***キャッチされていない例外 'NSInvalidArgumentException'、理由: - [__ NSDictionaryM CGImage]:インスタンスに送信されたセレクタが0x60000024a6e0 ' の理由でアプリケーションを終了しています。 @Monang Champaneri –

+0

.mのフルソースを送ってください –

+0

ur emailidまたはskype idを与えてください。 @Monang Champaneri –

関連する問題