iOS开发 东芝WifiSD卡 读取单反相机照片
因项目中需要读取东芝wifiSDcard中的图片,结合网上的资料 写出了一下Demo 做下记录
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface VZWifiSDCardImage : NSObject
@property (nonatomic,copy) NSString *imagePath; //图片地址
@property (nonatomic,copy) NSString *imageName; //图片名称
@end
@interface VZWifiSDCardFolder : NSObject
@property (nonatomic,copy) NSString *folderName; //文件名称
@property (nonatomic,strong) NSMutableArray<VZWifiSDCardImage *> *floderimages; //文件夹下的图片
@end
@interface VZWifiSDCardImageManager : NSObject
//获取东芝WifiSDcard上面的图片内容
+(NSMutableArray<VZWifiSDCardFolder *> *)getImagesForWifiSDCard;
@end
NS_ASSUME_NONNULL_END
#import "VZWifiSDCardImage.h"
@implementation VZWifiSDCardImage
@end
@implementation VZWifiSDCardFolder
@end
@implementation VZWifiSDCardImageManager
+(NSArray<VZWifiSDCardFolder *> *)getImagesForWifiSDCard{
NSString *baseURL = @"http://192.168.0.1/command.cgi?op=100&DIR=/DCIM";
NSString *basePath = @"http://192.168.0.1/DCIM";
NSURL *allData = [NSURL URLWithString:baseURL];
//以字符串的状态读出来DCIM的文件夹下的所有内容
NSString *stringOfFilesICDCIM = [NSString stringWithContentsOfURL:allData encoding:NSUTF8StringEncoding error:NULL];
//获得DCIM下所有的文件夹
NSArray *array_files = [stringOfFilesICDCIM componentsSeparatedByString:@"\r\n"];
//提前声明。
NSMutableArray <VZWifiSDCardFolder *> *folderArray = [[NSMutableArray alloc]init];
//分别遍历各个文件夹。
[array_files enumerateObjectsUsingBlock:^(NSString * _Nonnull obj_folderNmae, NSUInteger idx, BOOL * _Nonnull stop) {
//筛选符合条件的进行遍历
BOOL eligible = [self returnBOOLValueWithString:obj_folderNmae];
if (eligible) {
//再分别筛选他们的字段。
NSArray *array_atts = [obj_folderNmae componentsSeparatedByString:@","];
//文件夹名称就是第2个字段。
NSLog(@"%@/%@",array_atts.firstObject,array_atts[1]);
//进行存储文件夹的名字
VZWifiSDCardFolder *folder = [[VZWifiSDCardFolder alloc]init];
folder.folderName = array_atts [1]; // 100CANON 文件夹的名字。。。。。。
//再次遍历取出该文件夹下的文件路径。。。(二级路径);
NSURL *urlForFolderInfo = [NSURL URLWithString:[baseURL stringByAppendingPathComponent:folder.folderName]];
//取出该文件夹下的文件路径
NSString *imageDCIM = [NSString stringWithContentsOfURL:urlForFolderInfo encoding:NSUTF8StringEncoding error:NULL];
NSArray *array_image = [imageDCIM componentsSeparatedByString:@"\r\n"];
folder.floderimages = [[NSMutableArray alloc]init];
[array_image enumerateObjectsUsingBlock:^(NSString * _Nonnull obj_image, NSUInteger idx, BOOL * _Nonnull stop) {
if ([self returnBOOLTwoWithString:obj_image]) {
//取得文件的名称
NSArray *VZimages = [obj_image componentsSeparatedByString:@","];
VZWifiSDCardImage *image = [[VZWifiSDCardImage alloc] init];
image.imageName = VZimages[1];
image.imagePath = [basePath stringByAppendingFormat:@"/%@/%@", folder.folderName, image.imageName];
[folder.floderimages addObject:image];
}
}];
if (folder.folderName != nil) {
[folderArray addObject:folder];
}
}
}];
return folderArray;
}
+(BOOL)returnBOOLValueWithString:(NSString *)folderName{
if (folderName.length > 0 && ![folderName containsString:@"WLAN"] && ![folderName containsString:@".JPG"] && ![folderName containsString:@"TSB"] && ![folderName containsString:@"EOSMISC"]) {
return YES;
}
return NO;
}
+(BOOL)returnBOOLTwoWithString:(NSString *)stringOfImageAttributes{
if (stringOfImageAttributes.length > 0 && ![stringOfImageAttributes containsString:@"WLAN"] && [stringOfImageAttributes.uppercaseString containsString:@".JPG"]) {
return YES;
}
return NO;
}
@end
在ViewController里面的内容
#import "ViewController.h"
#import "XJTableViewCell.h"
#import <SDWebImage/SDWebImage.h>
#import "VZWifiSDCardImage.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView; //表格视图
@property (nonatomic,strong) NSMutableArray<VZWifiSDCardFolder *> *arrayOfFoldersWithImagesInFlashair; //照片信息
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(XJTableViewCell.class) bundle:nil] forCellReuseIdentifier:NSStringFromClass(XJTableViewCell.class)];
self.tableView.rowHeight = 100;
[self getData];
}
-(void)getData{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
self.arrayOfFoldersWithImagesInFlashair = [VZWifiSDCardImageManager getImagesForWifiSDCard];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self getData];
// });
});
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
XJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(XJTableViewCell.class)];
VZWifiSDCardImage * modele = self.arrayOfFoldersWithImagesInFlashair[indexPath.section].floderimages[indexPath.row];
[cell.icon sd_setImageWithURL:[NSURL URLWithString:modele.imagePath] placeholderImage:nil];
cell.imageName.text = modele.imageName;
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arrayOfFoldersWithImagesInFlashair[section].floderimages.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.arrayOfFoldersWithImagesInFlashair.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.arrayOfFoldersWithImagesInFlashair[section].folderName;
}
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
@end
另附上在网上搜索到的资源