iPhone - NSData from local file's URL

前端 未结 5 1393
说谎
说谎 2021-01-30 19:39

I have a NSURL object which gives me the path of a local file (in documents folder). I want to populate an NSData object with the contents of this file

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 19:59

    To get this work you just need to do:

    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];
    

提交回复
热议问题