ios圖片操作

先看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(00, 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.rowspentDate];

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
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //獲取Documents文件夾目錄    
  5.     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
  6.     NSString *documentPath = [path objectAtIndex:0];    
  7.     //指定新建文件夾路徑    
  8.     NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];    
  9.     //創建ImageFile文件夾    
  10.     [[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];    
  11.     //保存圖片的路徑   
  12.     self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"];  
  13.      
  14. }  
  15.   
  16. -(void)viewWillAppear:(BOOL)animated{  
  17.     [super viewWillAppear:YES];  
  18.     //根據圖片路徑載入圖片  
  19.     UIImage *image=[UIImage imageWithContentsOfFile:self.imagePath];  
  20.     if (image == nil) {  
  21.         //顯示默認  
  22.         [changeImg setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];  
  23.     }else {  
  24.         //顯示保存過的  
  25.         [changeImg setBackgroundImage:image forState:UIControlStateNormal];  
  26.     }  
  27. }  
  28.   
  29. - (void)dealloc {  
  30.     [imagePath release];  
  31.     [changeImg release];  
  32.     [super dealloc];  
  33. }  
  34. - (IBAction)changeImage:(id)sender {  
  35.     UIActionSheet *myActionSheet = [[UIActionSheet alloc]                      
  36.                                     initWithTitle:nil  
  37.                                     delegate:self  
  38.                                     cancelButtonTitle:@"取消"  
  39.                                     destructiveButtonTitle:nil  
  40.                                     otherButtonTitles: @"從相冊選擇", @"拍照",nil];   
  41.     [myActionSheet showInView:self.view];  
  42.     [myActionSheet release];   
  43. }  
  44. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  45.     switch (buttonIndex) {  
  46.         case 0:  
  47.             //從相冊選擇  
  48.             [self LocalPhoto];  
  49.             break;  
  50.         case 1:  
  51.             //拍照  
  52.             [self takePhoto];  
  53.             break;  
  54.         default:  
  55.             break;  
  56.     }  
  57. }  
  58. //從相冊選擇  
  59. -(void)LocalPhoto{  
  60.     UIImagePickerController *picker = [[UIImagePickerController alloc] init];    
  61.     //資源類型爲圖片庫  
  62.     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  63.     picker.delegate = self;  
  64.     //設置選擇後的圖片可被編輯  
  65.     picker.allowsEditing = YES;  
  66.     [self presentModalViewController:picker animated:YES];  
  67.     [picker release];  
  68. }  
  69.   
  70. //拍照  
  71. -(void)takePhoto{  
  72.     //資源類型爲照相機  
  73.     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;  
  74.     //判斷是否有相機  
  75.     if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){  
  76.         UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
  77.         picker.delegate = self;  
  78.         //設置拍照後的圖片可被編輯  
  79.         picker.allowsEditing = YES;  
  80.         //資源類型爲照相機  
  81.         picker.sourceType = sourceType;  
  82.         [self presentModalViewController:picker animated:YES];  
  83.         [picker release];  
  84.     }else {  
  85.         NSLog(@"該設備無攝像頭");  
  86.     }  
  87. }  
  88. #pragma Delegate method UIImagePickerControllerDelegate    
  89. //圖像選取器的委託方法,選完圖片後回調該方法    
  90. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{  
  91.       
  92.     //當圖片不爲空時顯示圖片並保存圖片  
  93.     if (image != nil) {    
  94.         //圖片顯示在界面上  
  95.         [changeImg setBackgroundImage:image forState:UIControlStateNormal];  
  96.           
  97.         //以下是保存文件到沙盒路徑下  
  98.         //把圖片轉成NSData類型的數據來保存文件  
  99.         NSData *data;  
  100.         //判斷圖片是不是png格式的文件  
  101.         if (UIImagePNGRepresentation(image)) {  
  102.             //返回爲png圖像。  
  103.             data = UIImagePNGRepresentation(image);  
  104.         }else {  
  105.             //返回爲JPEG圖像。  
  106.             data = UIImageJPEGRepresentation(image, 1.0);  
  107.         }  
  108.         //保存  
  109.         [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];  
  110.           
  111.     }    
  112.     //關閉相冊界面  
  113.     [picker dismissModalViewControllerAnimated:YES];  
  114. }  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章