2013-12-11 94 views
-4

我有下面的代碼,用於在用相機拍照後在iOS應用程序中存儲圖像。圖像需要有一個可識別的名稱,以便以後可以爲每個聯繫人檢索。我怎麼能傳遞給它一個參數或兩個參數,這樣文件名就會變成firstname_lastname.jpg而不是newImage.jpg?Objective-C發送參數到void函數

-(void)saveIMAGE { 

    NSData *imgView = UIImageJPEGRepresentation(imageView.image, 1.0); 

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docPath = [path objectAtIndex:0]; 

    imgPathString = [docPath stringByAppendingPathComponent:@"newImage.jpg"]; 

    [imgView writeToFile:imgPathString atomically:NO]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image Saved" message:@"The Image Was Saved." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; 
} 
+0

有人能至少說明向下票等等我至少知道我在問我的問題時做錯了什麼? –

+0

使用像:' - (void)saveImage:(NSString *)imageName { ... imgPathString = [docPath stringByAppendingPathComponent:imageName]; ... }' –

+0

謝謝,爲什麼倒票? –

回答

1

更新此行

imgPathString = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.jpg",firstname,lastName]]; 

修改你的方法,如:

- (void) saveImage :(NSString *)firstname withLastName :(NSString *)lastName 
{ 

     NSData *imgView = UIImageJPEGRepresentation(imageView.image, 1.0); 

     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *docPath = [path objectAtIndex:0]; 

     imgPathString = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.jpg",firstname,lastName]]; 

     [imgView writeToFile:imgPathString atomically:NO]; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image Saved" message:@"The Image Was Saved." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 

} 

,只是改變調用

[self saveImage:@"Scott" withLastName:@"Rowley"]; 
+0

太棒了,非常感謝。 –

+0

我用變量運行調用,但它運行良好,再次感謝。 [self saveImage:artist_Name withLastName:artist_LastName]; –