33

UIImage画像をオブジェクトに設定するにはどうすればよいですか。例えば:

UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@"anyImageName"]];

私のUIImageオブジェクトは.hファイルで宣言され、メソッドで割り当てられます。メソッドinitでイメージを設定する必要がありviewDidLoadます。何か案は?

4

12 に答える 12

58
UIImage *img = [UIImage imageNamed:@"anyImageName"];

imageNamed:
指定されたファイル名に関連付けられた画像オブジェクトを返します。

    + (UIImage *)imageNamed:(NSString *)name

パラメータ
name
ファイルの名前。画像が初めて読み込まれる場合、メソッドはアプリケーションのメインバンドルで指定された名前の画像を検索します。
戻り値
指定されたファイルの画像オブジェクト。メソッドが指定された画像を見つけられなかった場合はnil。

説明
このメソッドは、指定された名前の画像オブジェクトをシステムキャッシュで検索し、存在する場合はそのオブジェクトを返します。一致する画像オブジェクトがまだキャッシュにない場合、このメソッドは指定されたファイルから画像データをロードしてキャッシュし、結果のオブジェクトを返します。

于 2012-06-07T12:11:50.390 に答える
11

UIImageView を作成し、それに UIImage を追加します。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image Name"]] ;

次に、ビューに追加します。

[self.view addSubView: imageView];
于 2012-06-07T12:17:02.993 に答える
2

これに従ってください

UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
[imgview setImage:[UIImage imageNamed:@"YourImageName"]];
[imgview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgview];
于 2017-01-10T13:06:53.970 に答える
1

この行にエラーがあります:

[img setImage:[UIImage imageNamed@"anyImageName"]]; 

そのはず:

[img setImage:[UIImage imageNamed:@"anyImageName"]]; 
于 2012-06-07T12:13:33.363 に答える
1
UIImage *img = [UIImage imageNamed@"aImageName"];
于 2012-06-07T12:14:02.573 に答える
1

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.

于 2012-06-07T12:14:22.527 に答える
0

次の方法で UIImage オブジェクト eiter に Image を設定できます。

  1. UIImage *imageObject = [UIImage imageNamed:@"imageFileName"];しかし、このようにオブジェクト を初期化するUIImageことで、画像は常にキャッシュメモリに保存されます。nilimageObject=nil;

これに従う方が良いです:

  1. 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];
于 2012-11-30T18:39:25.163 に答える
0

UIImage を割り当てることはできません。これは不可能です。UIImageView を割り当てた適切なコード:

UIImageView *_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"something.png"]] ;

描画画像:

CGContextDrawImage(context, rect, _image.image.CGImage);
于 2015-09-30T09:24:58.847 に答える
0

vikingosgundo が言ったように、使用する[UIImage imageNamed:image]とイメージがキャッシュされ、メモリが消費されることに注意してください。したがって、多くの場所で同じ画像を使用する予定がない限り、画像をロードする必要がありますimageWithContentsOfFile:imageWithData:

これにより、メモリが大幅に節約され、アプリが高速化されます。

于 2012-06-07T12:16:15.893 に答える
0

最初に 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];
于 2012-09-22T04:49:24.923 に答える