2014-09-26 2 views
0

ViewController에서 PFFile의 하위 클래스로 전송할 때 데이터가 손실되는 데 현재 문제가 있습니다. 전달되는 데이터는 사용자 프로필에 업로드 할 이미지 데이터입니다.싱글 톤 iOS에 데이터 전달

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
// Access the uncropped image from info dictionary 
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

// Dismiss controller 
[picker dismissViewControllerAnimated:YES completion:nil]; 

// Resize image 
_focusedImage.image = image; 


NSData *imageData = UIImageJPEGRepresentation(image, 0.05f); 
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData]; 
[[imageUpload uploadImage] setImagePFFile:imageFile]; 
} 

이보기에 imageFile에 로그가 올바르게 인쇄됩니다 : 여기에 이미지를 선택하는 코드이다. 나는이 점에 도착하면

+ (imageUpload *) uploadImage 
{ 
static imageUpload*_sharedImageUpload = nil; 
_sharedImageUpload = [[self alloc] init]; 
_sharedImageUpload.imageData = [[NSData alloc] init]; 

PFUser *user = [PFUser currentUser]; 
_sharedImageUpload.imagePFFile = [[PFFile alloc] init]; 

PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:_sharedImageUpload.imageData]; 


[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    if (!error) 
    { 
     [user setObject:imageFile forKey:@"image"]; 
     [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (!error) 
      { 
       NSLog(@"This should be the profile image upload"); 
      } 
      else 
      { 
       NSLog(@"Something went wrong: %@", error); 
      } 
     }]; 
    } 
}]; 

return _sharedImageUpload; 
} 

, 시스템은 단지 빈 파일 (0 바이트)를 구문 분석에 업로드되는 동영상 : 내 싱글 톤 클래스 imageUpload uploadImage을 통해 데이터를 전달 그러나,이 같은 데이터 구조가 모습입니다. 이름 지정은 올바르며 데이터베이스의 올바른 위치에 있지만 파일 자체의 데이터가 손실되는 선상의 어딘가에 있습니다. 나는 이유를 알 수 없다. 누구든지 어떤 제안이 있습니까?

+0

@jefflovejapan 나는 아직도 비교적 새로운 편이다. 무슨 말이에요? –

답변

6

당신이 혼란스러운 대상과 방법 인 것처럼 보입니다. 원하는 것은 이미지를 업로드하는 메서드/함수를 가진 싱글 톤 객체입니다. 나는 이것이 당신이 찾고있는 것이라고 생각합니다 :

//ImageUploader.h 

#import <Foundation/Foundation.h> 

@interface ImageUploader : NSObject 
+ (instancetype)uploader; 
- (void)uploadImageFile:(PFFile *)aFile; 
@end 

//ImageUploader.m 

#import "ImageUploader.h" 

@implementation ImageUploader 
+ (instancetype)uploader { 
    static ImageUploader * _uploader = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     _uploader = [[self alloc] init]; 
    }); 

    return _uploader; 
} 

-(void)uploadPFFile:(PFFile *)imageFile{ 
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     if (!error) 
     { 
      [user setObject:imageFile forKey:@"image"]; 
      [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
       if (!error) 
       { 
        NSLog(@"This should be the profile image upload"); 
       } 
       else 
       { 
        NSLog(@"Something went wrong: %@", error); 
       } 
      }]; 
     } 
    }]; 
} 
@end 

[[ImageUploader uploader]uploadImageFile:someFile]을 호출하여 호출합니다.

+0

감사합니다. 네 말이 맞아, 나는 거기에서 잠깐 객체와 방법을 혼란스러워했다. 매우 감사! –

+0

문제 없습니다. 방금'saveInBackground' 메쏘드가'PFFile'에 정의되어 있다는 것을 알게되었습니다. 이것은 당신의 업 로더가 실제로 아무것도하지 않는다는 것을 의미합니다. 대신 파일을 처음 생성 할 때'saveInBackground'를 호출 할 수 있습니다. – jefflovejapan

관련 문제