ios图片操作
2012-12-30 23:01
344 查看
先看UIImagePickerControllerDelegate的協定,主要是利用iOS內建的圖片選取控制器 |
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
取得照片後的處理,範例 |
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];// UIImage *cmpImg = [appDelegate scaleImage:image toScale:kImageScaleRate];//縮圖 NSData *blobImage = UIImageJPEGRepresentation(cmpImg, kImageCompressRate);//圖片壓縮為NSData [self dismissModalViewControllerAnimated:YES]; [self updateImage:blobImage withIndexPath:indexPath_];//更新圖片(自定義函數) } |
叫起圖片選取器 |
-(void)snapImage:(id)sender { UIImagePickerController *ipc = [[UIImagePickerController alloc]init]; ipc.sourceType = UIImagePickerControllerSourceTypeCamera;//圖片來源 ipc.videoQuality = UIImagePickerControllerQualityTypeLow; ipc.delegate = self; ipc.allowsEditing = NO; [self presentModalViewController:ipc animated:YES]; } |
縮圖函數(自定義) |
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize)); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } |
儲存至資料庫※圖片傳入時已是NSData |
-(void)updateImage:(NSData*)image withIndexPath:(NSIndexPath*)indexPath { NSDateFormatter *dateformat = [[NSDateFormatter alloc]init]; [dateformat setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate* spentDate = [(SpentMoney*)[dailyContent objectAtIndex:indexPath.row] spentDate]; NSString *date = [NSString stringWithString:[dateformat stringFromDate: spentDate]]; //圖片傳入的時候,已經是NSData了,所以只要單純寫入即可 BOOL sucess = [fmdb executeUpdate:@"update spentMoney set contentImage =? where spentDate=?",image, date]; if (!sucess) { [appDelegate showMessageWith:@"fail to insert image" andMessage:nil]; } dateformat = nil; [self fetchData]; } |
自資料庫讀取,範例是tableView的Cell資料呈現的內容,看紅色字部份 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; SpentMoney *spentMoney = [dailyContent objectAtIndex:indexPath.row]; NSDateFormatter *dateformat = [[NSDateFormatter alloc]init]; dateformat.dateFormat = @"HH:mm:ss"; cell.textLabel.text = [dateformat stringFromDate: spentMoney.spentDate]; cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",spentMoney.money]; //圖片存至資料庫時是用NSData,讀取也只要用imageWithData把圖片讀取出來 cell.imageView.image = [UIImage imageWithData:spentMoney.contentImage]; return cell; dateformat = nil; } //add another author's passage 使用UIImagePickerController打开图片库和相机选择图片修改头像的主要方法如下, 声明:这个是iphone版本的,ipad版本的使用这个不行,因为iPad要用UIPopover才可以。 效果图: [html] view plaincopy - (void)viewDidLoad { [super viewDidLoad]; //获取Documents文件夹目录 NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath = [path objectAtIndex:0]; //指定新建文件夹路径 NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"]; //创建ImageFile文件夹 [[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil]; //保存图片的路径 self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; //根据图片路径载入图片 UIImage *image=[UIImage imageWithContentsOfFile:self.imagePath]; if (image == nil) { //显示默认 [changeImg setBackgroundImage:[UIImage imageNamed:@"user_photo@2x.png"] forState:UIControlStateNormal]; }else { //显示保存过的 [changeImg setBackgroundImage:image forState:UIControlStateNormal]; } } - (void)dealloc { [imagePath release]; [changeImg release]; [super dealloc]; } - (IBAction)changeImage:(id)sender { UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"从相册选择", @"拍照",nil]; [myActionSheet showInView:self.view]; [myActionSheet release]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0: //从相册选择 [self LocalPhoto]; break; case 1: //拍照 [self takePhoto]; break; default: break; } } //从相册选择 -(void)LocalPhoto{ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; //资源类型为图片库 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //设置选择后的图片可被编辑 picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; [picker release]; } //拍照 -(void)takePhoto{ //资源类型为照相机 UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; //判断是否有相机 if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //设置拍照后的图片可被编辑 picker.allowsEditing = YES; //资源类型为照相机 picker.sourceType = sourceType; [self presentModalViewController:picker animated:YES]; [picker release]; }else { NSLog(@"该设备无摄像头"); } } #pragma Delegate method UIImagePickerControllerDelegate //图像选取器的委托方法,选完图片后回调该方法 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{ //当图片不为空时显示图片并保存图片 if (image != nil) { //图片显示在界面上 [changeImg setBackgroundImage:image forState:UIControlStateNormal]; //以下是保存文件到沙盒路径下 //把图片转成NSData类型的数据来保存文件 NSData *data; //判断图片是不是png格式的文件 if (UIImagePNGRepresentation(image)) { //返回为png图像。 data = UIImagePNGRepresentation(image); }else { //返回为JPEG图像。 data = UIImageJPEGRepresentation(image, 1.0); } //保存 [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil]; } //关闭相册界面 [picker dismissModalViewControllerAnimated:YES]; } |
相关文章推荐
- ios开发之--从相机或相册选取图片,并带删除操作的demo
- IOS图片操作
- iOS绘图手势、图片裁剪、滚动效果、数据操作等源码
- [IOS]网络操作+图片的下载和读取+json数据读取
- IOS-图片操作集合
- [iOS]手势操作实现图片的缩放
- iOS:Documents目录下文件的操作(缓存图片为例)
- [IOS]网络操作+图片的下载和读取+json数据读取
- iOS图片实现可拉伸不变形的处理操作
- iOS中加载HTML数据,并点击图片或链接进行相应的操作
- ios webview点击图片看大图效果及相应手势操作
- IOS:实现对图片的双击放大、移动、捏合操作
- IOS-图片操作集合
- iOS UIWebView 图片自适应以及取消长按放大等操作
- iOS中加载HTML数据,并点击图片或链接进行相应的操作
- iOS开发中图片的一些处理操作(背景色,透明度,合成,大小)
- IOS自定义图片浏览器,支持浏览多张图片,点击,双击,两手指缩放操作,并显示加载进度条
- IOS-图片操作集合 待更新
- ios 图片操作
- iOS中在指定视图内进行代码截屏操作,并返回特定的图片