2011-10-20 21 views
1

巨大而奇怪的問題: 我用我決定的名字(天+隨機數)拍照並將它們保存在我的應用程序中,並將圖片的地址存儲在我的「 imagePath「NSString變量。 直到這個方法結束,我可以訪問「imagePath,這是一個全局變量的內容;但是當我嘗試在另一個方法中使用它時,我應該將此地址保存在sqlite數據庫中,它給我EXC_BAD_ACCESS。 ,另外,如果我嘗試看看其用 「的NSLog(@」 %@內容 「ImagePath的);」。它crashs 一些想法NSString使我的應用程序崩潰...也許

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
[picker dismissModalViewControllerAnimated:YES]; 

    int r = arc4random() % 9999; 
    NSDate *date = [NSDate date]; 
    NSString *photoName = [dateNameFormatter stringFromDate:date]; 
    photoName = [photoName stringByAppendingString:[NSString stringWithFormat:@"%d", r]]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %@.png", photoName]]; 

    UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    // ----- CODE FOR SCALE THE IMAGE ----- // 

    // ----- ----- - END CODE - ----- ----- // 

    NSData *webData = UIImagePNGRepresentation(picture); 
    CGImageRelease([picture CGImage]); 
    [webData writeToFile:imagePath atomically:YES]; 
    NSLog(@"%@", imagePath); 
    imgPicker = nil; 
} 

回答

2

的問題是這樣的:

imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %@.png", photoName]]; 

分配imagePath到自動釋放物體。這意味着它將作爲在事件循環週期結束時發生的漏極的一部分被釋放。

既然你不創建內存,但你希望它流連,你必須retain它:

[imagePath retain]; 

你需要什麼,以確定是當這片保留的內存可以發佈。由於我們不確切知道您的應用程序的工作方式,因此決定取決於您。如果你調用此方法不止一次,你可能會想釋放它是這樣:

if (imagePath) { 
    [imagePath release]; 
} 
imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %@.png", photoName]]; 
[imagePath retain]; 

如果該對象只應作爲長期生存作爲你的選擇器控制器,你應該釋放它在dealloc方法:

- (void)dealloc { 
    [imagePath release]; 
    // Other releases 
    [super dealloc]; 
} 

否則,如果這應該爲你的應用程序的生命週期生存,不用擔心在dealloc釋放它。 (但是如果你不止一次地分配它,你仍然應該在釋放之前釋放它)。當應用程序終止時,內存將被操作系統回收。

+0

太棒了!那太完美了!非常感謝。還有一件事:我如何使用圖像的完整路徑將圖像設置爲UIImage? (這僅僅是打算 - >/Users/Sk *****/Library/Application Support/iPhone Simulator/4.3.2/Applications/15977219-DEFE-407A-BB80-72E188E18DD2/Documents/20-10-20111746。 png) – Oiproks

+0

你應該在單獨的問題中提出這個問題。我可以回答,但評論部分不是這個地方。 :) –

-1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *imgPathWithOutDir = [[NSString alloc] initWithFormat:@" %@.png", photoName]; 
    imagePath = [documentsDirectory stringByAppendingPathComponent:imgPathWithOutDir]; 

    UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    // ----- CODE FOR SCALE THE IMAGE ----- // 

    // ----- ----- - END CODE - ----- ----- // 

    NSData *webData = UIImagePNGRepresentation(picture); 
    CGImageRelease([picture CGImage]); 
    [webData writeToFile:imagePath atomically:YES]; 
    NSLog(@"%@", imagePath); 
    [imgPathWithOutDir release]; 
    imgPicker = nil; 
+1

此代碼中的括號過多! (即使在編輯之後!) – Simon

1

你可以試試這個...

imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %@.png", photoName]]; 

[imagePath retain]; 

// Your code// 

[webData writeToFile:imagePath atomically:YES]; 
NSLog(@"%@", imagePath); 

[imagePath release]; 
+1

釋放dealloc中的方法不同。 – Ishu

+0

這個答案不正確。 OP將** imagePage **作爲實例變量存儲,以便可以在其他方法中使用。如果您在創建後立即將其釋放,則可能會釋放該對象,導致ivar處於損壞狀態。另外@Ishu,如果多次調用此方法,則在dealloc中保留** release **可能導致內存泄漏。 –

+0

@craig然後它應該像[imagePath release]; [imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@「%@。png」,photoName]]; [imagePath retain]; – Ishu

0

你的ImagePath存儲的值不歸您所有。從stringByAppendingPathComponent:你得到一個autoreleased對象。你保留它。 (順便說一句:你在開發ios5嗎?)

0

因爲imagePath是全局變量。那麼你可以在課堂上的任何地方使用它,但是當你存儲某個值的時候會發生什麼,而你並沒有保留它。

之後,它得到價值的方法,它釋放。所以請使用保留。這條線

imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %@.png", photoName]]; 

[imagePath retain]; 

並釋放它的dealloc中沒有任何方法,而你是不是要求保留任何其他的時間。