UIImage
画像をオブジェクトに設定するにはどうすればよいですか。例えば:
UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@"anyImageName"]];
私のUIImage
オブジェクトは.h
ファイルで宣言され、メソッドで割り当てられます。メソッドinit
でイメージを設定する必要がありviewDidLoad
ます。何か案は?
UIImage
画像をオブジェクトに設定するにはどうすればよいですか。例えば:
UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@"anyImageName"]];
私のUIImage
オブジェクトは.h
ファイルで宣言され、メソッドで割り当てられます。メソッドinit
でイメージを設定する必要がありviewDidLoad
ます。何か案は?
UIImage *img = [UIImage imageNamed:@"anyImageName"];
imageNamed:
指定されたファイル名に関連付けられた画像オブジェクトを返します。+ (UIImage *)imageNamed:(NSString *)name
パラメータ
name
ファイルの名前。画像が初めて読み込まれる場合、メソッドはアプリケーションのメインバンドルで指定された名前の画像を検索します。
戻り値
指定されたファイルの画像オブジェクト。メソッドが指定された画像を見つけられなかった場合はnil。説明
このメソッドは、指定された名前の画像オブジェクトをシステムキャッシュで検索し、存在する場合はそのオブジェクトを返します。一致する画像オブジェクトがまだキャッシュにない場合、このメソッドは指定されたファイルから画像データをロードしてキャッシュし、結果のオブジェクトを返します。
UIImageView を作成し、それに UIImage を追加します。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image Name"]] ;
次に、ビューに追加します。
[self.view addSubView: imageView];
これに従ってください
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
[imgview setImage:[UIImage imageNamed:@"YourImageName"]];
[imgview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgview];
この行にエラーがあります:
[img setImage:[UIImage imageNamed@"anyImageName"]];
そのはず:
[img setImage:[UIImage imageNamed:@"anyImageName"]];
UIImage *img = [UIImage imageNamed@"aImageName"];
may be:
UIImage *img = [[UIImage alloc] init];
and when you want to change the image:
img = [UIImage imageNamed:@"nameOfPng.png"];
but the object wasn't in the same place in the memory, but if you use the pointer, the same pointer will be point to the last image loaded.
次の方法で UIImage オブジェクト eiter に Image を設定できます。
UIImage *imageObject = [UIImage imageNamed:@"imageFileName"];
しかし、このようにオブジェクト
を初期化するUIImage
ことで、画像は常にキャッシュメモリに保存されます。nil
imageObject=nil;
これに従う方が良いです:
NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"imageFilename" ofType:@"imageFile_Extension"];
UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];
例: NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"png"];
UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];
UIImage を割り当てることはできません。これは不可能です。UIImageView を割り当てた適切なコード:
UIImageView *_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"something.png"]] ;
描画画像:
CGContextDrawImage(context, rect, _image.image.CGImage);
vikingosgundo が言ったように、使用する[UIImage imageNamed:image]
とイメージがキャッシュされ、メモリが消費されることに注意してください。したがって、多くの場所で同じ画像を使用する予定がない限り、画像をロードする必要がありますimageWithContentsOfFile:
。imageWithData:
これにより、メモリが大幅に節約され、アプリが高速化されます。
最初に UIImageView を宣言し、フレームを指定します
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake( 10.0f, 15.0f, 40.0f,40.0f )];
[imageView setBackgroundColor: [UIColor clearColor]];
[imageView setImage:[UIImage imageNamed:@"comments_profile_image.png"]];
[self.view addSubview: imageView];