首先,我从照片库中选择图像到 ALAsset 库,然后将图像存储在 ALAsset 库路径的文档目录中。
我正在使用此代码将图像存储在 ALAsset 库的文档目录中...它工作完美...现在我想在表视图中显示存储在文档目录中的所有图像..我该怎么做?
有谁能够帮助我??
将图像从 ALAsset 库导入到 NSdocument 目录的代码
for (int j=0; j<[assetArray count]; j++) {
ALAssetRepresentation *representation = [[assetArray objectAtIndex:j] defaultRepresentation];
NSString* filename = [documentPath stringByAppendingPathComponent:[representation filename]];
[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:filename append:YES];
[outPutStream open];
long long offset = 0;
long long bytesRead = 0;
NSError *error;
uint8_t * buffer = malloc(131072);
while (offset<[representation size] && [outPutStream hasSpaceAvailable]) {
bytesRead = [representation getBytes:buffer fromOffset:offset length:131072 error:&error];
[outPutStream write:buffer maxLength:bytesRead];
offset = offset+bytesRead;
}
[outPutStream close];
free(buffer);
}
之后我使用以下代码获取了目录的内容:
NSFileManager *manager = [NSFileManager defaultManager];
fileList = [manager directoryContentsAtPath:newDir];
它也可以工作...但是现在当我想显示文档目录中的图像时。它没有显示任何东西......
setImage.image=[UIImage imageNamed:[filePathsArray objectAtIndex:0]];
谁能帮忙啊,问题出在哪里????
- 我有一个疑问:
*将图像从 ALAsset 库导入到文档目录的正确方法吗???
这个答案是关于如何从文档目录中检索图像并将其显示到 UITableView.....
首先,您必须将文档目录中的所有图像获取到数组中......
-(void)viewWillAppear:(BOOL)animated
{
arrayOfImages = [[NSMutableArray alloc]init];
NSError *error = nil;
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath: stringPath error:&error];
for(int i=0;i<[filePathsArray count];i++)
{
NSString *strFilePath = [filePathsArray objectAtIndex:i];
if ([[strFilePath pathExtension] isEqualToString:@"jpg"] || [[strFilePath pathExtension] isEqualToString:@"png"] || [[strFilePath pathExtension] isEqualToString:@"PNG"])
{
NSString *imagePath = [[stringPath stringByAppendingString:@"/"] stringByAppendingString:strFilePath];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[arrayOfImages addObject:image];
}
}
}
}
之后 - 使用此数组,您可以在 uitableview 单元格中显示图像
请记住,在数组填满之前,不要在视图上添加表视图......
#pragma mark - UItableViewDelegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfImages count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tablView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//adjust your imageview frame according to you
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 470.0, 80.0)];
[imageView setImage:[arrayOfImages objectAtIndex:indexPath.row]];
[cell.contentView addSubview:imageView];
return cell;
}
现在运行它就可以工作了..
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)