画像の幅と高さを検出し、それらの寸法を次のコードに渡したいと考えています。
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(25, 5, imgView.size.width, imgView.size.width)];
私の構文に何か問題があるか、これを行う方法が間違っています。どんな助けでも素晴らしいでしょう、ありがとう
画像の幅と高さを検出し、それらの寸法を次のコードに渡したいと考えています。
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(25, 5, imgView.size.width, imgView.size.width)];
私の構文に何か問題があるか、これを行う方法が間違っています。どんな助けでも素晴らしいでしょう、ありがとう
行う:
UIImage *someImage = [UIImage imageNamed:@"someImage.png"];
UIImageView *someImageView = [[UIImageView alloc] initWithImage:someImage];
someImageView.frame = CGRectMake(0, 0, someImage.size.width, someImage.size.height);
これを試して
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
それがあなたを助けることを願っています
プロパティから画像のサイズを取得できUIImage
size
ます。たぶん次のようなもの:
UIImage *img = [UIImage imageNamed:@"image.png"];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(25, 5, img.size.width, img.size.width)];
または、次のことができます。
UIImageView *imgView=[[UIImageView alloc] initWithImage:img];
自動的にサイズ調整されます
画像ビューを割り当てて初期化したばかりなので、画像ビューの幅も高さもないと思われます。initWithFrame 初期化メソッドには float 値が必要なため、まだ決定されていないため、imgView にクエリを実行して幅と高さの値を返すことはできません。
通常、イニシャライザ initWithImage を使用します。次に、初期化後にイメージビューの幅と高さを変更します。
最大幅または高さを設定したい場合は、このコードを試してください。サイズを制御するだけでなく、画像ビューのフレームを動的に設定できます。
CGSize size = image.size;
// Here Maximum width of image is 220
if (size.width > 220)
{
size.height /= (size.width / 220);
size.width = 220;
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
imageView.image = image;
// For layer mask or round corner
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;