裁剪 UIImagePicker 并输出到 CollectionView 单元格
Posted
技术标签:
【中文标题】裁剪 UIImagePicker 并输出到 CollectionView 单元格【英文标题】:Crop UIImagePicker and output to CollectionView cell 【发布时间】:2013-12-29 19:22:27 【问题描述】:我正在尝试创建一个相机按钮,该按钮拍摄一张方形图片,然后填充 CollectionViewController 单元格。现在,我在裁剪图片并将其输出到单元格时遇到了一些问题。在注释的代码行中“将图像裁剪为正方形:我收到一条错误消息,指出“图像”是未声明的标识符。如何裁剪图像并将其设置为单元格?
- (IBAction)cameraButtonClicked:(id)sender
if (![UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)])
UIAlertView *cameraAlertView = [[UIAlertView alloc] initWithTitle:@"Camera Not Available" message:@"There is no camera on this device which really defeats the purpose of this game. We suggest you get an iDevice with a camera." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[cameraAlertView show];
else
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
if (ipc.sourceType == UIImagePickerControllerSourceTypeCamera)
//Create camera overlay
CGRect f = ipc.view.bounds;
f.size.height -= ipc.navigationBar.bounds.size.height;
CGFloat barHeight = (f.size.height - f.size.width) / 2;
UIGraphicsBeginImageContext(f.size);
[[UIColor colorWithWhite:0 alpha:.5] set];
UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, barHeight), kCGBlendModeNormal);
UIRectFillUsingBlendMode(CGRectMake(0, f.size.height - barHeight, f.size.width, barHeight), kCGBlendModeNormal);
UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *overlayIV = [[UIImageView alloc] initWithFrame:f];
overlayIV.image = overlayImage;
[ipc.cameraOverlayView addSubview:overlayIV];
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
//Crop the image to a square
CGSize imageSize = image.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
if (width != height)
CGFloat newDimension = MIN(width, height);
CGFloat widthOffset = (width - newDimension) / 2;
CGFloat heightOffset = (height - newDimension) / 2;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), NO, 0.);
[image drawAtPoint:CGPointMake(-widthOffset, -heightOffset)
blendMode:kCGBlendModeCopy
alpha:1.];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
#pragma mark ImagePicker Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self dismissViewControllerAnimated:YES completion:nil];
@end
【问题讨论】:
【参考方案1】:将您的“作物”代码移到此行下方:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
在
didFinishPickingMediaWithInfo
您需要使用新裁剪的图像更新您的数据源。然后,您可以在 UICollectionView 上调用 reloadData
或 insertItemsAtIndexPaths:
以使其反映新添加的图像。我会支持调用后一种方法,因为它比使用reloadData
重新加载整个集合更有效(当集合中有很多图像时)。
UICollectionView
引用是here。
【讨论】:
这有助于解决裁剪问题,但是将图像设置为 CollectionView 单元格呢? 我修改了答案,希望对您有所帮助。以上是关于裁剪 UIImagePicker 并输出到 CollectionView 单元格的主要内容,如果未能解决你的问题,请参考以下文章