Objective-C将参数发送到void函数

时间:2013-12-11 05:25:56

标签: objective-c

我有以下代码,用于在使用相机拍照后在iOS应用中存储图像。图像需要具有可识别的名称,以便稍后可以为每个联系人检索该名称。我如何传递一个或两个参数,以便文件名最终会像firstname_lastname.jpg而不是newImage.jpg?

  -(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];
}

1 个答案:

答案 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"];