我正在为 iphone 创建一个相机应用程序,但我面临一个问题。
我希望能够提供我拍摄的照片,将其存储在应用程序的私有目录中,并为其提供自定义名称(当然是通过代码)。例如,我拍了一张照片,我想将这张照片命名为 mmddyyyyhhmmssXYZ 并将事物图像存储在 myApp/images 目录中。
请让我知道这是否可能。如果可能的话,任何有关的建议或教程都会非常有帮助。
作为 iphone 应用程序开发的新手......我现在实际上对此一无所知。
谢谢
我正在为 iphone 创建一个相机应用程序,但我面临一个问题。
我希望能够提供我拍摄的照片,将其存储在应用程序的私有目录中,并为其提供自定义名称(当然是通过代码)。例如,我拍了一张照片,我想将这张照片命名为 mmddyyyyhhmmssXYZ 并将事物图像存储在 myApp/images 目录中。
请让我知道这是否可能。如果可能的话,任何有关的建议或教程都会非常有帮助。
作为 iphone 应用程序开发的新手......我现在实际上对此一无所知。
谢谢
这是我的代码见下文。
-(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
}
我希望它可以帮助你......!!!!
将图像写入应用程序私有目录的功能-
-(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];
}