2013-10-25 5 views
1

나는 최근 메모리 충돌 데와 내가 배열을 비우 실패하고 있음을 의심하고 추가 유지, 이것은배열은 단지 객체와 메모리 충돌

- (void)viewDidLoad 
{ 
    allImagesArray = [[NSMutableArray alloc] init]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *[email protected]"Bottoms"; 
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location]; 
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath]; 
    collectionBottoms.delegate =self; 
    collectionBottoms.dataSource=self; 
    for(NSString *str in directoryContent){ 
     NSLog(@"i"); 
     NSString *finalFilePath = [fPath stringByAppendingPathComponent:str]; 
     NSData *data = [NSData dataWithContentsOfFile:finalFilePath]; 
     if(data) 
     { 
      UIImage *image = [UIImage imageWithData:data]; 
      [allImagesArray addObject:image]; 
      NSLog(@"array:%@",[allImagesArray description]); 

     }} 
} 

호 내가를 확보 할 수 있습니다 배열 내 코드입니다 배열에있는 객체를 해제하여 메모리를?

+0

필요없는 경우 이미지를 어레이에서 제거 하시겠습니까? –

+0

정확히 ... 배열에있는 한 그들은 살아있을 것입니다. – borrrden

+0

내가 할 수있는 곳을 찾을 수 없다. 코드를 넣어 두었다.'[allImagesArray removeAllObjects]; NSLog (@ "% @", allImagesArray);}'그러나 좋은 장소를 찾을 수 없습니다. 먼저 그림을 저장하여 문서에 저장하고 싶습니다. 그러나 위 코드를 넣으면 방금 찍은 그림이 삭제됩니다. – interdatega

답변

0

allImagesArray이 이미지 배열을 사용하는 경우보기가 표시되면이 코드를 viewWillAppear 또는 viewDidAppear 방법으로 UIViewController에 넣습니다.

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    allImagesArray = [[NSMutableArray alloc] init]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *[email protected]"Bottoms"; 
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location]; 
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath]; 
    collectionBottoms.delegate =self; 
    collectionBottoms.dataSource=self; 
    for(NSString *str in directoryContent){ 
     NSLog(@"i"); 
     NSString *finalFilePath = [fPath stringByAppendingPathComponent:str]; 
     NSData *data = [NSData dataWithContentsOfFile:finalFilePath]; 
     if(data) 
     { 
      UIImage *image = [UIImage imageWithData:data]; 
      [allImagesArray addObject:image]; 
      NSLog(@"array:%@",[allImagesArray description]); 

     } 
    } 
} 

, 그때 UIViewController의 viewWillDisappear/viewDidDisappear 방법에 놓습니다.

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 

    [allImagesArray removeAllObjects]; 
    //[allImagesArray release]; // (Uncomment) Call this only if you are not using ARC. 
} 

또는, , 빠른 더러운하지만 안전 수정 (allImagesArray의 사용량을 모른 채)입니다 - 당신은 PLIST에서 이미지 배열을 저장

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Remove previously added objects. 
    if (allImagesArray != nil && allImagesArray.count > 0) { 
      [allImagesArray removeAllObjects]; 
    } 
    // Allocate memory if not done earlier (i.e. first time). 
    if (allImagesArray == nil) { 
     allImagesArray = [[NSMutableArray alloc] init]; 
    } 

    // Rest of your code as stated in your question 
} 
+0

나는이 줄을 맨 아래에 쓰는 코드를 얻지 못하고 왜 viewWillappear가 viewdidload가 아닌지? – interdatega

+0

위의 대체 접근법에 대한 코드를 명확히했습니다. 'viewWillAppear'은 매번 화면에 표시되고'viewDidDisappear'는 스크린이 제거 될 때마다 호출됩니다. 이것들은'UIViewController' 메소드입니다. 자세한 내용은 공식 문서를 참조하십시오 - https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html – Ashok

1

. 저장 한 후에 배열을 다시 초기화하면 배열의 메모리가 지워집니다. 그럼 ARC를 사용하고 있습니까? ARC가 더 편하게 사용할 수 있기 때문입니다. 그런 다음 배열에서 fromAllObject를 다시 초기화하거나 -viewWillDisappear에서 제거하십시오.

0

안녕 개체 작업이 끝나면 메모리를 놓습니다. 여기에 당신을위한 코드가 있습니다.

- (void)viewDidLoad 
{ 
    allImagesArray = [[NSMutableArray alloc] init]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *[email protected]"Bottoms"; 
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location]; 
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath]; 
    collectionBottoms.delegate =self; 
    collectionBottoms.dataSource=self; 
    for(NSString *str in directoryContent) 
    { 
     NSString *finalFilePath = [fPath stringByAppendingPathComponent:str]; 
     NSData *data = [NSData dataWithContentsOfFile:finalFilePath]; 
     if(data) 
     { 
      UIImage *image = [UIImage imageWithData:data]; 
      [allImagesArray addObject:image]; 
      NSLog(@"array:%@",[allImagesArray description]); 
     image = nil; 

     } 
    finalFilePath=nil; 
    data=nil; 
    } 

paths= nil; 
documentsDirectory= nil; 
location= nil; 
fPath= nil; 
directoryContent = nil; 
} 
+0

이것은 도움이 될 것 같지만 메모리를 줄이지 만 아직도 많은 사진을 찍은 후에 쌓입니다. 카메라 코드 나 다른 것에 문제가 있습니까? 이것은 내 전체 코드 https://github.com/laryufk/Helper/tree/master입니다. 카메라에 문제가 있습니다. – interdatega

+0

작업이 끝나면 객체를 없애거나 해제해야합니다. 따라서 코드에서 메모리를 해제했는지 확인할 수 있습니다. – Nirmalsinh