有没有大神能够解答一下如何往沙盒中创建一系jpg的文件,创建文件名为image1.jpg、image2.jpg、image3.jpg这样,在沙盒的document文件夹下生成文件,谢谢啦!!! //图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//关键问题
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil];
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.jpg"];
} else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
} else {
NSLog(@"Uknown asset type");
}
}
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//关键问题
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil];
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.jpg"];
} else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
} else {
NSLog(@"Uknown asset type");
}
}
解决方案 »
- 可以用java开发苹果程序吗
- 如何保存类中的字符串变量,求解,请教各位
- 第三方如何获得Facebook,twitter用户的帐号
- 求iPhone软件开发创意!!!!!!!
- 弱引用指向的对象释放,测试打印有疑问
- initWithWindowNibName 的使用疑问
- 关于在IOS下的 zip解压与压缩
- 研究便利函数返回的对象是否是autorelease时,发现的奇怪现象求解释
- sqlite3更新数据库问题 SQL执行成功但数据库数据不改变
- XCODE4.6建立新项目无法调用IOS模拟器
- ios 真机调试,点击控件(UITextField.UIButton...)就崩溃,拔掉数据线就没问题
- 服务器返回的数据不知道怎么解压,求大神救我
int i=0;
for (NSDictionary *dict in info) {
.....
filePath = [[NSString alloc]initWithFormat:@"%@/image%d.jpg",DocumentsPath, i];
i++;
} else
.....