UIImage *img = [[UIImage alloc] initWithContentsOfFile:@"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];
Podłączyłem już połączenia dla UIImageView (_headersView) w IB. Obraz, który chcę załadować, znajduje się również w strukturze drzewa mojego projektu.
Czy tworzę UIImage we właściwy sposób?
Odpowiedzi:
8 dla odpowiedzi № 1Jeśli plik znajduje się w pakiecie, możesz go po prostu użyć
UIImage *img = [UIImage imageNamed:@"contactHeader"];
_headersView = [[UIImageView alloc] initWithImage:img];
Jeśli obraz znajduje się w katalogu dokumentów, możesz go użyć
// Get the path to your documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add the file to the end of the documents path
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"contactHeader.png"];
UIImage *img = [[UIImage alloc] imageWithContentsOfFile:filePath];
_headersView = [[UIImageView alloc] initWithImage:img];
[img release]; img = nil;
2 dla odpowiedzi nr 2
Spróbuj tego
-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:@"png"];
return path;
path = nil;
}
self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:@"123"]];