pathForResource? without using extension (Iphone)

后端 未结 5 1290
醉话见心
醉话见心 2020-12-31 04:22

Here is what I\'m doing, when I create an image with the path in the bundle:


UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathFo         


        
5条回答
  •  -上瘾入骨i
    2020-12-31 05:12

    - (NSData *)applicationDataFromFile:(NSString *)fileName {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
        NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
        return myData;
    }
    

    taken from http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW21

    Could be easily adapted if you wanted it to return a UIImage instead of an NSData.

    Also, you don't say if you are saving the images to the documents directory, or adding them to your app bundle before compiling. because if it's the latter, you can use [UIImage imageNamed:(NSString *)filename] to get the image. It expects an extension as part of the file-name.

提交回复
热议问题