需求是把上面的效果改成下面的效果,记住一个坑,那就是图片上传到服务器的时候,一定是要原图的数组,而不是发帖时显示的数组,改了一下午,各种百度裁剪图片的代码,搞不懂为什么有的代码是错误的还在网上,历尽千辛万苦,终于找到了关键的一段,好激动,赶快分享,如果你有什么比裁剪更好的思路来修改发布时的不压缩图片,请分享给我哦😉😉😉😉😉😉😉😉😉
#pragma mark ELCImagePickerControllerDelegate Methods
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
//原图片
// NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
//剪切图片
NSMutableArray *shearImages = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
//原图数组
[self.imageArray addObject:image];
//裁剪图片
image = [self croppedImage:image];
//裁剪数组
[shearImages addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
// [imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.contentMode = UIViewContentModeScaleToFill;
imageview.layer.masksToBounds = YES;
imageview.clipsToBounds = YES;
} else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
} else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
//原图数组
[self.imageArray addObject:image];
//裁剪图片
image = [self croppedImage:image];
//裁剪数组
[shearImages addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
imageview.contentMode = UIViewContentModeScaleToFill;
imageview.layer.masksToBounds = YES;
imageview.clipsToBounds = YES;
} else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
} else {
NSLog(@"Uknown asset type");
}
}
[self.childChooseArray addObjectsFromArray:shearImages];
[self chooseUI];
// [_scrollView setPagingEnabled:YES];
// [_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}
//裁剪图片
- (UIImage *)croppedImage:(UIImage *)image
{
if (image)
{
float min = MIN(image.size.width,image.size.height);
CGRect rectMAX = CGRectMake((image.size.width-min)/2, (image.size.height-min)/2, min, min);
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rectMAX);
UIGraphicsBeginImageContext(rectMAX.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, CGRectMake(0, 0, min, min), subImageRef);
UIImage *viewImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
CGImageRelease(subImageRef);
return viewImage;
}
return nil;
}
参考了这个链接,感谢这篇文章的作者http://blog.csdn.net/lyhuzhu/article/details/8699689