1.本地文件图片
将所需的图片导入项目,直接拖拽!
然后请看具体代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//更改控制台背景
//使用本地图像
NSData *data1=[[NSData alloc]initWithContentsOfFile:@"/Users/stjy/Desktop/Pictures/kaong.jpg"];//使用已经图片的具体路径,讲图片的内容以存入data1中(二进制形式)
UIImage *image=[UIImage imageWithData:data1];//使用数据来创建图像
[ self.view setBackgroundColor:[UIColor colorWithPatternImage:image]];//设置控制台背景
//使用网络地址
NSURL *url1=[NSURL URLWithString:@"http://imgsrc.baidu.com/forum/pic/item/8e335c6046165cd28db10da9.jpg"];//创建一个地址对象url1
NSData *data3=[NSData dataWithContentsOfURL:url1];//获取地址的数据
UIImage *image1=[UIImage imageWithData:data3];//使用已有数据创建图像
[ self.view setBackgroundColor:[UIColor colorWithPatternImage:image1]];//设置控制台背景
//沙盒(获取其内文件)
//查找该程序的bundle地址
NSBundle *bd=[NSBundle mainBundle];
NSLog(@"%@",[bd bundlePath] );//输出该应用程序的bundle路径
NSLog(@"哈哈%@",[bd pathForResource:@"357435647,2259912874" ofType:@"jpg"]);
//下面是 使用已有的图片路径来更改控制台背景方法1
NSData *data5=[[NSData alloc]initWithContentsOfFile:@"/Users/stjy/Library/Application Support/iPhone Simulator/5.1/Applications/BBA6EF3E-9D66-4DD0-9C2C-35B048A3F60B/文件和网络.app/357435647,2259912874.jpg"] ;//创建图像地址
UIImage *image5=[UIImage imageWithData:data5];//创建图像
[ self.view setBackgroundColor:[UIColor colorWithPatternImage:image5]];
//设置控制台背景
// 方法2.
NSData *data6= [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"357435647,2259912874.jpg"]];
NSLog(@"%@",data6);
UIImage *image6=[UIImage imageWithData:data6];//创建图像
[ self.view setBackgroundColor:[UIColor colorWithPatternImage:image6]];
//设置控制台背景
//Nsfilemanager
//判断地址是否正确
if ([[ NSFileManager defaultManager]fileExistsAtPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"357435647,2259912874.jpg"]])
//返回值是bool值
{
NSLog(@"存在");
}
else{
NSLog(@"不存在");
}
NSData *da=[[NSData alloc] initWithContentsOfFile:[[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:@"2860380766,4111071421.jpg"]];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithData:da]]];
// 其实就是这样的思路view-->color-->image-->data-->path-->
NSData *da2=[NSData dataWithContentsOfFile:@"/Users/stjy/Pictures/Pictures/2860380766,4111071421.jpg"];
NSLog(@"%d",[da2 length]);
}