保存自定义相机捕获图像iOS Xcode
我是iPhone新手,我想将图像保存到相册中&我想要图像路径。 我已经这么做了 现在,我想要一个自定义的图像名称&用这个名称存储图像。 我用了下面的代码,但我没有得到任何图像到相册 谁能告诉我哪里错了保存自定义相机捕获图像iOS Xcode,ios,iphone,xcode,camera,save,Ios,Iphone,Xcode,Camera,Save,我是iPhone新手,我想将图像保存到相册中&我想要图像路径。 我已经这么做了 现在,我想要一个自定义的图像名称&用这个名称存储图像。 我用了下面的代码,但我没有得到任何图像到相册 谁能告诉我哪里错了 #pragma mark - Image Picker Controller delegate methods - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInf
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
// Todo to use custom name comment this line.
//UIImageWriteToSavedPhotosAlbum(self.imageView.image,nil,nil,nil);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
});
}
在我的一个项目中,我做了以下工作:
let directory = "yourDirectoryName"
let fileName = "file.png"
let directoryPath = NSHomeDirectory() + "/Library/Caches/\(directory)"
do {
try NSFileManager.defaultManager().createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
UIImagePNGRepresentation(image)?.writeToFile(directoryPath+fileName), atomically: true)
} catch let error as NSError {}
代码在Swift 2.3中工作。我希望,这就是你想要的 在我的一个项目中,我做了以下工作:
let directory = "yourDirectoryName"
let fileName = "file.png"
let directoryPath = NSHomeDirectory() + "/Library/Caches/\(directory)"
do {
try NSFileManager.defaultManager().createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
UIImagePNGRepresentation(image)?.writeToFile(directoryPath+fileName), atomically: true)
} catch let error as NSError {}
代码在Swift 2.3中工作。我希望,这就是你想要的 验证NSData和UIImage对象是否正确,将“原子”设置为“是”,检查我使用的以下代码:
//Save your image with a completion selector
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//Completion selector
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo
{
if(error)
{
NSLog(@"save error :%@", [error localizedDescription]);
}
else if (!error)
{
PHAsset *asset = [self fetchImageAsset];
}
}
//Fetch image based on create date or whatever naming system you follow
- (nullable PHAsset *)fetchImageAsset
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
return lastAsset;
}
验证NSData和UIImage对象是否正确,将“原子”设置为“是”,检查我使用的以下代码:
//Save your image with a completion selector
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//Completion selector
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo
{
if(error)
{
NSLog(@"save error :%@", [error localizedDescription]);
}
else if (!error)
{
PHAsset *asset = [self fetchImageAsset];
}
}
//Fetch image based on create date or whatever naming system you follow
- (nullable PHAsset *)fetchImageAsset
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
return lastAsset;
}
您可以使用以下自定义名称存储图像:
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//save image in Document Derectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Get Path : %@",documentsDirectory);
//create Folder if Not Exist
NSError *error = nil;
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSString *customePhotoName=@"MyPhotoName";
NSString* path= [dataPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",customePhotoName]];
NSData* imageData = UIImagePNGRepresentation(chosenImage);
[imageData writeToFile:path atomically:YES];
NSLog(@"Save Image Path : %@",path);
您可以使用以下自定义名称存储图像:
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//save image in Document Derectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Get Path : %@",documentsDirectory);
//create Folder if Not Exist
NSError *error = nil;
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSString *customePhotoName=@"MyPhotoName";
NSString* path= [dataPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",customePhotoName]];
NSData* imageData = UIImagePNGRepresentation(chosenImage);
[imageData writeToFile:path atomically:YES];
NSLog(@"Save Image Path : %@",path);
我认为这是不可能的。你为什么要这样做?你的目标是什么?添加自定义图像的名称?@sohil。这是客户要求使用cient1、client2等存储图像。我应该遵循天气,建议,但在调用图像时感到困惑。您想将图像存储在照片库或个人文档目录中吗?我想使用自定义名称存储图像。哪种方法都很简单。个人或图库。如果您有自定义名称的存储图像,您希望将图像存储在文档目录中,我认为这是不可能的。您为什么这样做?添加自定义图像名称的目标是什么?@sohil。这是客户要求使用cient1、client2等存储图像。我应该遵循天气,建议,但在调用图像时感到困惑。您想将图像存储在照片库或个人文档目录中吗?我想使用自定义名称存储图像。哪种方法都很简单。个人或图库。如果您有自定义名称的存储图像,则希望将图像存储在文档目录是。但是我应该在didfinishPickingMediawithInfo函数之后或者在其他任何地方调用它吗?是的,我在didfinishPickingMediawithInfo函数中使用它是的。但是,我应该在didfinishPickingMediawithInfo函数之后或者在其他任何地方调用它吗?是的,我在didfinishPickingMediawithInfo函数中使用它。我对此非常陌生。但我没明白你的意思。我理解了第一部分保存UIImageWriteToSavedPhotoAlbum,但没有得到任何其他内容。我想我需要先学习更多关于iPhone的课程。谢谢你的回答。“选择器”只是指一个函数,“完成选择器”是指将在任务完成时运行的方法。您可以在“UIImageWriteToSavedPhotosAlbum”中将此方法名称作为参数传递,这样您就可以在将图像写入指定路径时获得错误(NSError),从而了解出了什么问题。PHAsset也是一个objective c类,用于保存和获取媒体资产。请投票表决。干杯你有iPhone初学者教程的参考资料吗?去youtube看看“斯坦福大学iOS”频道,它们现在是用Swift语言编写的,但是所有旧的“Objective-c”教程也可以在iTunes频道免费获得。你应该对面向对象编程有一个基本的认识:林克:我对这个很陌生。但我没明白你的意思。我理解了第一部分保存UIImageWriteToSavedPhotoAlbum,但没有得到任何其他内容。我想我需要先学习更多关于iPhone的课程。谢谢你的回答。“选择器”只是指一个函数,“完成选择器”是指将在任务完成时运行的方法。您可以在“UIImageWriteToSavedPhotosAlbum”中将此方法名称作为参数传递,这样您就可以在将图像写入指定路径时获得错误(NSError),从而了解出了什么问题。PHAsset也是一个objective c类,用于保存和获取媒体资产。请投票表决。干杯你有iPhone初学者教程的参考资料吗?去youtube看看“斯坦福大学iOS”频道,它们现在是用Swift语言编写的,但是所有旧的“Objective-c”教程也可以在iTunes频道免费获得。你应该有一个面向对象编程的基本概念:链接:谢谢。我已经对此投了赞成票。如果这行得通的话。我会接受的。我检查了我的iPhone。我没有拍到照片。在哪里可以看到捕获图像?我需要使用相同的UIImageWriteToSavedPhotosAlbum???@NovusMobile它不会存储在您的iphone照片库中,而是存储在文档目录文件夹中。如果要在照片库中存储图像,请使用“UIImageWriteToSavedPhotosAlbum(图像,无,无,无);”然后编写此代码,但它不会在照片上显示图像名称。您能解释一下自定义名称的要求吗?完成,谢谢@Sohil。接受答案。希望你们对这个问题也能这么做。谢谢。我已经对此投了赞成票。如果这行得通的话。我会接受的。我检查了我的iPhone。我没有拍到照片。在哪里可以看到捕获图像?我需要使用相同的UIImageWriteToSavedPhotosAlbum???@NovusMobile它不会存储在您的iphone照片库中,而是存储在文档目录文件夹中。如果要在照片库中存储图像,请使用“UIImageWriteToSavedPhotosAlbum(图像,无,无,无);”然后编写此代码,但它不会在照片上显示图像名称。您能解释一下自定义名称的要求吗?完成,谢谢@Sohil。接受答案。希望你们能对这个问题做同样的回答。