我能够将我的文件数组保存在我的NSDocumentDirectory中。下面是我使用的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
//NSData *imageData = UIImagePNGRepresentation(image);
[UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];我的问题是如何将这种格式的@"Images%d.png"图像加载到下面的代码中:
.h
@property (strong, nonatomic) NSMutableArray *images;.m
self.images = ??????发布于 2012-05-29 14:35:13
下面是从bundle中读取图像名称的代码。请将捆绑包路径修改为文档目录路径。代码片段将为您提供已排序的图像名称数组,并使您不必使用for循环来读取名称。
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *unsortedImages = [dirContents filteredArrayUsingPredicate:filter];
NSArray *sortedImages = [unsortedImages
sortedArrayUsingComparator:
^NSComparisonResult (id obj1, id obj2){
return [(NSString*)obj1 compare:(NSString*)obj2
options:NSNumericSearch];
}];友好的建议:避免在NSDocumentDirectory目录中存储任何可下载的数据/图像,因为应用程序可能会被拒绝。请改用NSCachesDirectory。请在苹果的开发者门户上阅读此post和相关文档。
干杯!!
阿马尔。
发布于 2013-09-26 18:35:34
用于将图像保存到NSDocumentDirectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];//m_nImageIndex
NSString *imagename = [NSString stringWithFormat:@"savedImage%d.png",m_nImageIndex];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imagename];
// Get PNG data from following method
NSData *myData = UIImagePNGRepresentation(image);
// It is better to get JPEG data because jpeg data will store the location and other related information of image.
[myData writeToFile:savedImagePath atomically:NO];
//[UIImageJPEGRepresentation(image ,1) writeToFile:savedImagePath atomically:NO];用于检索图像
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"savedImage%d.png",indexPath.row]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
cell.backgroundView = [[UIImageView alloc] initWithImage:img];发布于 2012-05-31 00:48:48
您正在将png扩展名另存为jpg文件。我相信这应该是可行的:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.jpg", i]];
ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];
// to load the image later:
UIImage *imageFromDisk = [UIImage imageWithContentsOfFile:savedImagePath];https://stackoverflow.com/questions/10792873
复制相似问题