2012-03-30 4 views
0

저는 Objective-C에 매우 익숙하며 초보자 문제가 있습니다. 나는 사진 갤러리처럼 행동해야하는 영역을 가진 응용 프로그램을 가지고 있습니다. 사용자가 카메라 롤에서 사진을 선택하면 사진이 UIImageViews에 표시됩니다. 내가 선택한 이미지를 저장하려고합니다. 9 개의 UIImageView가 있고 문제는 각 UIImageView에 대해 다른 사진을 선택하고 닫고 다시 실행하면 나머지 8 개의 UIImageViews는 첫 번째 이미지보기에 저장된 사진을 표시합니다. 차라리 그들 모두보다 같은 사진을 표시, 내가 올바른 사진을 표시 할 UIImageViews를 얻기 위해 변경할 필요가 무엇인지 알아 내려고 노력하고있어iOS : 사진 저장 문제

- (NSString *)dataFilePath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                 NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return [documentsDirectory stringByAppendingPathComponent:kFilename9]; 
} 

- (void)applicationDidEnterBackground:(UIApplication*)application { 
    NSLog(@"Image on didenterbackground: %@", imageView); 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView5.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView6.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView7.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView8.image)]; 
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView9.image)]; 

    [self.imageData writeToFile:[self dataFilePath] atomically:YES]; 
    NSLog(@"The image is: %@", [[imageView image] description]); 
    NSLog(@"dataFilePath is: %@", [self dataFilePath]); 

} 

- (void)viewDidLoad 
{ 

    NSString *filePath = [self dataFilePath]; 
    NSLog(@"FilePath: %@", filePath); 
    NSLog(@"Image: %@", imageView); 
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
     NSData *vdlData = [[NSData alloc] initWithContentsOfFile:filePath]; 
     imageView.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView2.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView3.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView4.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView5.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView6.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView7.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView8.image = [[UIImage alloc] initWithData:vdlData]; 
     imageView9.image = [[UIImage alloc] initWithData:vdlData]; 
    } 


    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(applicationDidEnterBackground:) 
               name:UIApplicationDidEnterBackgroundNotification 
               object:app]; 

    [super viewDidLoad]; 

} 

: 여기에 내가 함께 일하고 있어요 코드입니다. 이것은 아마도 간단한 수정이지만, 도움을 주시면 대단히 감사하겠습니다.

+2

vdlData ...에 저장된 동일한 이미지를 사용하여 각 imageView를 할당하고 하나의 경로 (dataFilePath)에 저장하기 전에 각 이미지를 동일한 변수에 저장 (서로 덮어 쓰기)하고 있는지 확인 했습니까? – David

+0

오우 와우, 내가 어떻게 그리웠 니? 고맙습니다. 모든 변수를 하나의 경로에 저장하는 것이 좋을까요? 아니면 각 변수마다 다른 경로를 사용해야합니까? – John

+1

여기 나는 대답으로 만들 것이다. – David

답변

2

좋아, 여기에 내가 할 것입니다 방법은 다음과 같습니다

사용 NSUserDefaults가 변경 가능한 배열로 이미지를 저장 :

ViewController.h

@property(retain) NSUserDefaults *user; 

의 ViewController에게.m

@synthesize user; 

- (void)viewWillAppear:(BOOL)animated 
{ 

    self.user = [NSUserDefaults standardUserDefaults]; 

편집

NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy]; 
    while(array == nil) 
    { 
     [self.user setObject:[NSMutableArray arrayWithObject:@""] forKey:@"images"] 
     array = [[self.user objectForKey:@"images"]mutableCopy]; 
     NSLog(@"%@",@"attempting to create an array to store the images in"); 
    } 

최종 편집 }

- (void)applicationDidEnterBackground:(UIApplication*)application { 
    NSLog(@"Image on didenterbackground: %@", imageView); 
    NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]]; 

    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView5.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView6.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView7.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView8.image)]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView9.image)]; 

    [self.user setObject:array forKey:@"images"]; 

} 

- (void)viewDidLoad 
{ 
    NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy]; 

편집

01 2,376,875,532,

최종 편집

UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationDidEnterBackground:) 
              name:UIApplicationDidEnterBackgroundNotification 
              object:app]; 

    [super viewDidLoad]; 

} 

- (void)viewDidUnload 
{ 
    self.user = nil; 
} 

이 방법을 사용하면, 그들이 접근을 쉽게 저장 될 이미지 나 데이터가 손실되지 않으며 앱을 업데이트 할 경우에도 그들은 사라지지 않습니다.

건배!

+0

David, 도와 줘서 고마워! – John

+1

문제 없습니다. 나는 당신이 다른 질문으로 당신을 도왔다는 것을 깨달았습니다. 하하, 작은 사이트. 비록 그렇지 않더라도. – David

+0

롤 예, 당신은 사람을 했어요, 당신은 평생을 구했습니다. 질문이 하나 더 있습니다. 코드를 구현하지만 어떤 이유로 이미지를 저장하지 않아도 어디에서나 반환해야합니까? 다시 터크! – John

2

솔루션을 시작하기 전에이 작업을 수행하는 방식이 옳지 않다는 것을 경고해야합니다. iOS 개발을 처음부터 배우기 시작하는 것이 좋습니다. 애플의 문서는 꽤 좋은 출발점이다. http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html

이제 질문에 답변하십시오. 여기서하는 일은 하나의 이미지 만 저장하는 것이지 전부 9 개를 저장하는 것이 아닙니다. 처리하는 각 이미지에 항상 self.imageData을 설정하고 이전 값을 덮어 쓰면 마지막 이미지 만 파일에 저장됩니다.

따라서 코드를 작동 시키려면 각 이미지보기에 imageData 개체를 사용하고 해당 데이터 개체를 파일에 써야합니다.

경우에 따라서는 복수의 객체 (예 : imageView, imageView2 ...)를 사용하는 대신 루프를 사용하여 코드를 최적화하는 것이 가장 좋습니다.

또한 메모리를 관리해야합니다. 예 : imageView.image은 할당되었지만 릴리스되지 않았습니다.

+1

-1 첫 번째 단락을 구체적인 조언으로 바꾸거나 삭제하십시오. 또한 가독성을 위해 소형 코드 블록을 표시하기 위해 백틱을 자유롭게 사용하십시오. 대답을 고칠 때 마이너스 1을 되돌릴 수 있습니다. –

+2

이제 괜찮습니다. –

1

음, 두 가지 문제점이 있습니다. 무엇보다 먼저 viewDidLoad에서 모든 이미지가 initWithData : vdlData ...를 얻고 있으므로 모두 동일한 데이터를 얻습니다. 그것이 모두 같은 이유입니다.

또한 저장하려고 할 때 에 계속해서 imageData의 값을 덮어 쓰고 있습니다 ... 저장하면 마지막으로 imageData에 할당 된 것입니다. NSArray을 생성하여 배열에 저장하려면 viewDidLoad으로 배열에 저장합니다.