gpt4 book ai didi

iphone - NSDocumentDirectory 加载图像

转载 作者:行者123 更新时间:2023-11-29 04:38:06 26 4
gpt4 key购买 nike

我能够将文件数组保存在我的 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 = ??????

最佳答案

用于将图像保存到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];

关于iphone - NSDocumentDirectory 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com