iPhone-ファイルプロパティ
-
30-09-2019 - |
質問
iPhoneを簡単なファイル共有の目的でペンドライブとして機能させるアプリケーションを作成しています。
最初の段階では、ディレクトリにいくつかのファイル(PNG、PDF、JPG、ZIP)があり、それらをTableViewに変換可能な配列の形式で表示しました。以下に示すように、テーブルビューに表示されます。
.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial
ファイルの名前を表示したいだけで、拡張機能を表示したくありません。 NSFIleManagerのファイルの拡張機能を抽出することが可能であることを知っています。しかしどうしたら良いかわからない。私のテーブルビューを次のように見せてくれるのを手伝ってください
.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial
第2段階には、ファイルの詳細なビューを次のように表示する詳細なviewcontrollerがあります
- ファイルサイズ
- ファイルタイプ
- 画像の場合、ImageViewで開く必要があります
- それが歌の場合、それはそれを再生する必要があります
したがって、各ファイルのFilePath、Filetype、Filesize ..などのプロパティを取得する必要があります。可能であれば、チュートリアルをご案内してください。私も、Mutablearrayをnsstringに変換し、StringBydeletingPathextensionなどの方法を呼び出す方法を知りません。私を助けてください。必要に応じてソースコードを送信することもできます。可能であれば、いくつかの例コードとチュートリアルをご覧ください。
解決
これは機能するはずです:)これは、nsstring *parentDirectoryのディレクトリ内のすべてのファイルを取得し、そのサイズを取得します。
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *filePaths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
error = nil;
}
for (NSString *filePath in filePaths) {
//filename without extension
NSString *fileWithoutExtension = [[filePath lastPathComponent] stringByDeletingPathExtension];
//file size
unsigned long long s = [[fm attributesOfItemAtPath:[parentDirectory stringByAppendingPathComponent:filePath]
error:NULL] fileSize];
UIImage *image = [UIImage imageNamed:[parentDirectory stringByAppendingPathComponent:filePath];];
//if image...
if(image){
//show it here
}
else{
//otherwise it should be music then, play it using AVFoundation or AudioToolBox
}
}
他のヒント
NSURLオブジェクトにファイル名があることを願っています。もしそうなら、次のコードを使用してファイル名のみを取得し、文字列からファイル拡張子を削除できます。
NSArray *fileName = [[fileURL lastPathComponent] componentsSeparatedByString:@"."];
NSLog(@"%@",[fileName objectAtIndex:0]);
所属していません StackOverflow