将图片存储在app目录中并为其指定自定义名称。

时间:2012-11-14 09:58:22

标签: iphone objective-c camera image-capture

我正在为iphone创建一个相机应用程序,我正面临一个问题。

我希望能够提供我拍摄的照片,存储在应用程序的私人目录中,并为照片提供自定义名称(当然通过代码)。例如,我拍了一张照片,我想将照片命名为mmddyyyyhhmmssXYZ并将物品图像存储在myApp / images目录中。

如果可能,请告诉我。如果可能的话,任何建议或教程都会非常有帮助。

成为iphone应用程序开发的新手......我现在对此毫无头绪。

由于

2 个答案:

答案 0 :(得分:1)

这是我的代码,见下文。

-(void)copyImagesToCache:(NSString*)imageURl
{

    NSFileManager *filemanager=[NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"MyIMages"];

            //now no need to use below single line of code.
//NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.png"];
//it just a example here , here you should make a source  path as you pic the images form the camera.

编辑:在ImagePicker中选择时,你应该建立一条路径。请参阅下面的ImagePickerViewControllerDelegate.i委托已经显示了

的方式
    if(![filemanager contentsOfDirectoryAtPath:folderPath error:nil]){
        [filemanager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];

    }
    NSString *destPath = [folderPath stringByAppendingPathComponent:@"image1.png"];//here you should create the dynamic name so that you can distinguish all images.
    NSError *err;
  BOOL isCopeid= [filemanager copyItemAtPath:srcPath toPath:destPath error:&err];
  if(isCopeid)
    NSLog(@"Copied");
}


  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:     (UIImage *)image editingInfo:(NSDictionary *)editingInfo
 { 
  myIMage.image = image;
 //here call the method whcih copy the image to cache
 [self copyImagesToCache: [editingInfo objectForKey:UIImagePickerControllerReferenceURL]]];  
 [here is the link which shown the  editingInfo's keys ][1] 

//remaining code goes here as it is   
}

我希望它可以帮助你...... !!!!

答案 1 :(得分:0)

将图片写入应用的私人目录的功能 -

-(void)writeImagetoPath
 {

    UIImage *myImage = //This is the UIImage you obtained from the camera or the photo library.

    //This is the path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"xyz.png",i]];//You can give any name to the path.

    //Now convert the uiimage to data before writing to file.
    NSData *imgData = UIImagePNGRepresentation(myImage);
    [imgData writeToFile:documentsPath atomically:YES];

}