macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

setImage:

ボタンに画像をセットして再描画します
-(void)setImage:(NSImage *)image:

解説

ボタンに画像をセットして再描画します。

chatgpt.com

返り値

( void )

なし

引数

( NSImage * )image

セットする画像

フレームワーク

ApplicationKit

クラス

NSButton

使用可能

10.0

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
	
	//開けるファイル拡張子の配列
    NSArray      *imgTypes    = [ NSArray arrayWithObject : @"tiff" ];
    //OpenPanelを作る
    NSOpenPanel  *opImage       = [ NSOpenPanel openPanel ];
    //Imageを作る
    NSImage      *img;
    //OpenPanelの結果のボタン番号
    int		  opRet;
	
	//OpenPanelでファイル選択   
    opRet = [ opImage runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
									  file : @"Pictures" //どのファイルを選択しておくか
									 types : imgTypes ];//選べるファイルタイプ
	
    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
        //NSImageを作ってファイルから読み込む
        img = [ [ NSImage alloc ] 
			   initWithContentsOfFile: [ opImage filename ] ];
		//ボタンにImageをセット
        [but1 setImage : img ];
        //but1の画像を取得してbut2にセット
        [but2 setImage : [but1 image] ];
	}
}

@end

SwiftUI

//
//  ContentView.swift
//  setImage_
//
//  Created  on 2025/02/24.
//  macOS 15

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
            // シンプルな画像ボタン
                        Button(action: {
                            print("画像ボタンが押されました")
                        }) {
                            Image(systemName: "play.circle.fill") // SF Symbols を使用
                                .resizable()
                                .frame(width: 50, height: 50)
                                .foregroundColor(.blue)
                        }

                        // 画像とテキストを組み合わせたボタン
                        Button(action: {
                            print("再生ボタンが押されました")
                        }) {
                            HStack {
                                Image(systemName: "play.fill")
                                Text("再生")
                            }
                            .padding()
                            .background(Color.blue)
                            .foregroundColor(.white)
                            .cornerRadius(10)
                        }
        }
        .padding()
    }
}

#Preview {
    ContentView()
}