iOS 圖片上傳處理 圖片壓縮 圖片處理

iOS 圖片上傳處理 圖片壓縮 圖片處理

提到從攝像頭/相冊獲取圖片是面向終端用戶的,由用戶去瀏覽並選擇圖片爲程序使用。在這裏,咱們須要過UIImagePickerController類來和用戶交互。使用UIImagePickerController和用戶交互,咱們須要實現2個協議。iphone

View Codeide

代碼以下複製代碼this

#pragma mark 從用戶相冊獲取活動圖片atom

- (void)pickImageFromAlbumspa

{orm

imagePicker = [[UIImagePickerController alloc] init];對象

imagePicker.delegate =self;圖片

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;ip

imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;開發

imagePicker.allowsEditing =YES;

[self presentModalViewController:imagePicker animated:YES];

}

咱們來看看上面的從相冊獲取圖片,咱們首先要實例化UIImagePickerController對象,而後設置imagePicker對象爲當前對象,設置imagePicker的圖片來源爲UIImagePickerControllerSourceTypePhotoLibrary,代表當前圖片的來源爲相冊,除此以外還能夠設置用戶對圖片是否可編輯。

View Code

代碼以下複製代碼

#pragma mark 從攝像頭獲取活動圖片

- (void)pickImageFromCamera

{

imagePicker = [[UIImagePickerController alloc] init];

imagePicker.delegate =self;

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

imagePicker.allowsEditing =YES;

[self presentModalViewController:imagePicker animated:YES];

}

//打開相機

- (IBAction)touch_photo:(id)sender {

// for iphone

UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;

pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];

}

pickerImage.delegate =self;

pickerImage.allowsEditing =YES;//自定義照片樣式

[self presentViewController:pickerImage animated:YES completion:nil];

}

以上是從攝像頭獲取圖片,和從相冊獲取圖片只是圖片來源的設置不同,攝像頭圖片的來源爲UIImagePickerControllerSourceTypeCamera。

在和用戶交互以後,用戶選擇好圖片後,會回調選擇結束的方法。

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

{

//初始化imageNew爲從相機中得到的--

UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

//設置image的尺寸

CGSize imagesize = imageNew.size;

imagesize.height =626;

imagesize.width =413;

//對圖片大小進行壓縮--

imageNew = [self imageWithImage:imageNew scaledToSize:imagesize];

NSData *imageData = UIImageJPEGRepresentation(imageNew,0.00001);

if(m_selectImage==nil)

{

m_selectImage = [UIImage imageWithData:imageData];

NSLog(@"m_selectImage:%@",m_selectImage);

[self.TakePhotoBtn setImage:m_selectImage forState:UIControlStateNormal];

[picker dismissModalViewControllerAnimated:YES];

return ;

}

[picker release];

}

//對圖片尺寸進行壓縮--

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize

{

// Create a graphics image context

UIGraphicsBeginImageContext(newSize);

// Tell the old image to draw in this new context, with the desired

// new size

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

// End the context

UIGraphicsEndImageContext();

// Return the new image.

return newImage;

}

圖片保存到本地document裏面--以及圖片格式的轉換

IOS開發之保存圖片到Documents目錄及PNG,JPEG格式相互轉換

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:@"public.image"]){

image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSData *data;

if (UIImagePNGRepresentation(image) == nil) {

data = UIImageJPEGRepresentation(image, 1);

} else {

data = UIImagePNGRepresentation(image);

}

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePath = [NSString stringWithString:[self getPath:@"image1"]];        //將圖片存儲到本地documents

[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:dataattributes:nil];

UIImage *editedImage = [[UIImage alloc] init];

editedImage = image;

CGRect rect = CGRectMake(0, 0, 64, 96);

UIGraphicsBeginImageContext(rect.size);

[editedImage drawInRect:rect];

editedImage = UIGraphicsGetImageFromCurrentImageContext();

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];

imageButton.frame = CGRectMake(10, 10, 64, 96);

[imageButton setImage:editedImage forState:UIControlStateNormal];

[self.view addSubview:imageButton];

[imageButton addTarget:self action:@selector(imageAction:)forControlEvents:UIControlEventTouchUpInside];

[ipc dismissModalViewControllerAnimated:YES];

} else {

NSLog(@"MEdia");

}

上面的代碼是當從相冊裏面選取圖片以後保存到本地程序沙盒,在上面咱們獲得的圖片中不可以獲得圖片名字,

以及不清楚圖片格式,因此這個時候咱們須要將其轉換成NSdata二進制存儲

image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSData *data;

if (UIImagePNGRepresentation(image) == nil) {

data = UIImageJPEGRepresentation(image, 1);

} else {

data = UIImagePNGRepresentation(image);

}

UIImagePNGRepresentation轉換PNG格式的圖片爲二進制,若是圖片的格式爲JPEG則返回nil;

[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:data attributes:nil];    將圖片保存爲PNG格式

[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil];  將圖片保存爲JPEG格式

咱們也能夠寫成下面的格式存儲圖片

NSString *pngImage = [filePath stringByAppendingPathComponent:@"Documents/image.png"];

NSString *jpgImage = [filePath stringByAppendingPathComponent:@"Documents/image.jpg"];

[data writeToFile:pngImage atomically:YES];

[data writeToFile:jpgImage atomically:YES];

相關文章
相關標籤/搜索