문제

나는있다 NSURL 로컬 파일의 경로를 제공하는 개체 (문서 폴더). 나는 채우고 싶다 NSData 이 파일의 내용이있는 객체. 사용해 보았습니다 dataWithContentsOfURL: 그러나 이것은 실패합니다. iPhone이기 때문에 파일이 존재한다는 것을 알고 있습니다 SDK 경로를 반환합니다.

누군가 내가 어떻게 얻을 수 있는지 말해 줄 수 있습니까? NSData 객체에서 URL 로컬 파일의?

감사.

도움이 되었습니까?

해결책

// Given some file path URL: NSURL *pathURL
// Note: [pathURL isFileURL] must return YES
NSString *path = [pathURL path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

다른 팁

이 작업을 수행하려면 다음과 같습니다.

NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"search" withExtension:@"png"];
NSString*stringPath = [imgPath absoluteString]; //this is correct  

//you can again use it in NSURL eg if you have async loading images and your mechanism 
//uses only url like mine (but sometimes i need local files to load)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
UIImage *ready = [[[UIImage alloc] initWithData:data] autorelease];

로컬 URL이 "파일 : //"로 시작하는지 확인하십시오.그러면 그냥 이것을해야 할 것입니다.

 NSData *fileData = [NSData dataWithContentsOfFile:fileURL.path];

Swift3의 경우 다음 URL을 다음 과정에서 도움을 받았습니다

(PhoneGap 플러그인을 사용하여 이미지를 캡처 한 후 경로를 따르고있었습니다.

파일 : ///var/mobile/containers/data/application/b64b06de-7976-45cf-9be2-661f0f309a06/tmp/abcded.jpg)

https://stackoverflow.com/a/41043447/6383908

videourl = url (string : urlstring)이면 videodata = 시도하자? Data (내용 : videourl) {// 여기에 Alamofire 코드 추가}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top