IOS开发之UIImageView

本文介绍了如何在iOS应用中设置启动界面,包括使用UIImageView显示图片、调整图片尺寸及位置等基本操作,并展示了如何通过不同内容模式实现图片的适应性显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

   

    

     _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    

    [_window setBackgroundColor:[UIColor whiteColor]];

    

    //==============UIImageView=================

    //UIImageView:UIView UIView的所有属性方法UIImageView都拥有

    //UIImageView专门用来显示图片的控件

    //1.创建一个UIImageView对象

    UIImageView *imageView = [[UIImageView alloc]

                              initWithFrame:CGRectMake(50, 50, 200, 200)];

    //2.设置背景颜色

    imageView.backgroundColor = [UIColor cyanColor];

    

    //3.显示在界面上;

    [_window addSubview:imageView];

    

       //UIIamge图片类

    //根据图片名创建图片(前提是图片已经添加到工程中)

    //拖到工程中的图片实质是在当前这个应用程序的安装包里面;

    //参数:图片的名字(如果图片是png图片,图片的后缀名可以省略,其他图片后缀必须加上);

    UIImage *image1 = [UIImage imageNamed:@"back2.jpg"];

    

    //根据文件路径来创建图片;

    //参数是图片文件的路径;拿到包中图片的路径;(可以拿到当前工程中任何文件的路径)

    //参数一:文件的名字

    //参数二:文件的后缀

    NSString *path = [[NSBundle mainBundle] pathForResource:

                      @"player_down_1"  ofType:@"png"];

    UIImage *image2 = [UIImage imageWithContentsOfFile:path];

    

    //==========获取图片的大小==============

    CGSize imageSize =  image1.size;

   

    //NSLog(@"%@",NSStringFromCGSize(imageSize));

    

    //两种创建图片方式的区别:

    //1.通过图片名直接创建:代码简单,imageNamed创建的图片的声明语法是整个工程结束,

    //而且如果是同一张图片创建的image对象是唯一的.

    //2.通过图片文件的路径来创建图片

    

    //4.设置图片(核心属性)

    [imageView setImage:image1];

    

    //5.设置内容模式

//    typedef NS_ENUM(NSInteger, UIViewContentMode) {

//        UIViewContentModeScaleToFill,

//(默认的模式:将图片进行缩放将图片整个显示在imageView;

//        UIViewContentModeScaleAspectFit,//将图片按照比例缩放完整显示在imageView

//        UIViewContentModeScaleAspectFill,//图片和imageView都按图片的比例缩放

//        UIViewContentModeRedraw,

//        UIViewContentModeCenter,//完整的将图片中间的显示出来

//        UIViewContentModeTop,

//        UIViewContentModeBottom,

//        UIViewContentModeLeft,

//        UIViewContentModeRight,

//        UIViewContentModeTopLeft,

//        UIViewContentModeTopRight,

//        UIViewContentModeBottomLeft,

//        UIViewContentModeBottomRight,

//    };

    

    [imageView setContentMode:UIViewContentModeScaleToFill];

    

    [_window makeKeyAndVisible];

    


    

    

    

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end


转载于:https://my.oschina.net/luhoney/blog/653870

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值