绘制图片

来源:互联网 发布:湖北有色金属软件下载 编辑:程序博客网 时间:2024/04/28 11:07
1.#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end2.////  AppDelegate.m//  ios12-1-jietu////  Created by 李伟 on 13-5-31.//  Copyright (c) 2013年 com.csdn. All rights reserved.//#import "AppDelegate.h"#import "FisrtViewController.h"@implementation AppDelegate- (void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];        //设置主的视图控制器    FisrtViewController *firstVc = [[FisrtViewController alloc] init];        self.window.rootViewController = firstVc;        [firstVc release];        self.window.backgroundColor = [UIColor whiteColor];    [self.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:.}@end3.////  FisrtViewController.h//  ios12-1-jietu////  Created by 李伟 on 13-5-31.//  Copyright (c) 2013年 com.csdn. All rights reserved.//#import <UIKit/UIKit.h>@interface FisrtViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>@end4.////  FisrtViewController.m//  ios12-1-jietu////  Created by 李伟 on 13-5-31.//  Copyright (c) 2013年 com.csdn. All rights reserved.//#import "FisrtViewController.h"@interface FisrtViewController ()@end@implementation FisrtViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}-(void)loadView{    //自定义UIView作为主视图    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];    //设定背景    view.backgroundColor  = [UIColor grayColor];    self.view = view;    [view release];        //定义图片对象    UIImage *image = [UIImage imageNamed:@"2"];    //定义矩形区域    //CGRect rect = CGRectMake(200, 200, 100, 100);       //*****************图片裁剪*********************//    //定义UIImageView 然后用于截图后的显示//    UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];//    //设定imgView显示的图片//    //开始裁剪成矩形区域大小//    imgView.image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect)];//    //imgView.image = image;  //显示原图//    //    [self.view addSubview:imgView];//    [imgView release];         //*****************图片缩放***************//    CGSize size = CGSizeMake(100, 100);//    UIImage *newImg = [self reSizePhoto:image andReSize:size];//    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];//    //设定为新裁剪的图片//    imgView.image  = newImg;//    //    [self.view addSubview:imgView];//    [imgView release];    //实现文件保存    //[self saveImageToApp];        //保存图片到相簿    //[self saveImageToPhotos];        //实现相册图片的选取、拍照等    UIButton *selPhoto = [UIButton buttonWithType:UIButtonTypeRoundedRect];    selPhoto.frame = CGRectMake(30, 50, 100, 40);    //设置标题    [selPhoto setTitle:@"选择图片" forState:UIControlStateNormal];    //设置方法    [selPhoto addTarget:self action:@selector(getImage) forControlEvents:UIControlEventTouchUpInside];        [self.view addSubview:selPhoto];    }-(void)getImage{   //定义UIImagePickerController    UIImagePickerController *picker = [[UIImagePickerController alloc] init];   //设置图片源(相簿)    //picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;    //使用相机获取:    picker.sourceType = UIImagePickerControllerSourceTypeCamera;   //设置代理    picker.delegate = self;    //设定可以编辑    picker.allowsEditing  = YES;   //打开拾取器界面    [self presentViewController:picker animated:YES completion:nil];}//使用图片拾取器要实现的代理方法//选择了图片以后触发的方法- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){    NSLog(@"用户选择 了照片了%@",editingInfo);    //定义图片显示控件    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, image.size.width*0.3, image.size.height*0.3)];    //设置显示控件显示的内容为用户选择的图片    imgView.image = image;        [self.view addSubview:imgView];    [imgView release];        //选择框消失    [picker dismissViewControllerAnimated:YES completion:nil];}//选择了图片拾取器取消按钮执行的方法- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{     NSLog(@"用户选择 了取消了");}-(void)saveImageToApp{    UIImage *image = [UIImage imageNamed:@"guoqi"];    //构建路径    NSString *strPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"saveimg.png"];    //保存png的图片到app下的Document/saveimg.png    [UIImagePNGRepresentation(image) writeToFile:strPath atomically:YES];}-(void)saveImageToPhotos{        UIImage *image = [UIImage imageNamed:@"guoqi"];    //保存图片到相簿    UIImageWriteToSavedPhotosAlbum(image,self,@selector(saveImageResult:andError:),nil);    }-(void)saveImageResult:(UIImage *)_img andError:(NSError *)_error{    if (_error != NULL) {        NSLog(@"保存失败!,errorinfo -- %@",_error);            }else{            NSLog(@"保存成功!");    }}-(UIImage *)reSizePhoto:(UIImage *) _img andReSize:(CGSize) _size{    //获取图片的上下文    UIGraphicsBeginImageContext(_size);    //进行重新绘制    [_img drawInRect:CGRectMake(0, 0, _size.width, _size.height)];    UIImage *reImage = UIGraphicsGetImageFromCurrentImageContext();    //移除粘顶的图片上下文    UIGraphicsEndImageContext();        return reImage;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

0 0