我使用UIImagePickerController
获取imageView的图像,如下所示
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGSize destinationSize1 = CGSizeMake(200, 200);
UIGraphicsBeginImageContext(destinationSize1);
[originalImage drawInRect:CGRectMake(0,0,destinationSize1.width,destinationSize1.height)];
UIImage *newImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
firstimage1.image = newImage1;
[picker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"image width is %f",firstimage1.image.size.width);
NSLog(@"image height is %f",firstimage1.image.size.height);
if (firstimage1.image.size.width==200 && firstimage1.image.size.height==200) {
UIAlertView *alertcrp=[[UIAlertView alloc]initWithTitle:@"message!" message:@"you want to crop this image" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertcrp addButtonWithTitle:@"Crop "];
[alertcrp show];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Button Index =%ld",(long)buttonIndex);
if (buttonIndex == 0)
{
NSLog(@"You have clicked Cancel");
}
else if(buttonIndex == 1)
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.allowsEditing = YES; // this will allow you to crop the image first
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
我们调整图像大小到200*200。现在我需要的是当用户上传图像.If图像上传成功,然后打开alertView“如果你想裁剪图像”。单击裁剪按钮后,我需要打开矩形裁剪视图。
请告诉我关于我的高级problem.Thanks的任何想法。
发布于 2014-11-05 13:34:37
你需要做的事情很简单。首先创建一个你想要裁剪的裁剪大小的UIScrollView
,你可以让它动态,如果你想通过平移手势等,但我已经做了恒定裁剪大小,所以我不会进入细节。接下来,创建一个与原始图像大小相同的UIImageView
,并将其image proprety
设置为该大小。还要将scorllview's content size
属性设置为与图像大小完全相同的值。允许弹跳有一些额外的效果。然后,在按下裁剪时,你只需要将想要裁剪的滚动视图的内容偏移和大小(即滚动视图的大小,即它的宽度和高度)传递给这个函数。
- (UIImage *)crop:(UIImage *)image inRect:(CGRect)cropRect
{
CGImageRef cropRef = CGImageCreateWithImageInRect(image.CGImage, cropRect);
UIImage* cropImage = [UIImage imageWithCGImage:cropRef];
CGImageRelease(cropRef);
return cropImage;
}
您可以按照上面的描述调用此方法,如下所示
UIImage * img = [[CommonFunctions sharedManager] crop:imgView.image inRect:CGRectMake(scrollView.contentOffset.x , scrollView.contentOffset.y , reqWidth, reqHeight)];
根据您的逻辑使rect用于裁剪可能存在错误。但你应该对做什么有大致的了解。如果有什么事情让你困惑,或者它是否有帮助,请告诉我,这样我也许可以进一步帮助你:)
发布于 2014-11-05 10:32:16
要调整图像的大小,请使用此命令
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[myImage drawInRect:CGRectMake(0, 0, 200, 200)]; // Size of your ne image
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
发布于 2014-11-05 10:58:26
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.allowsEditing = YES; // this will allow you to crop the image first
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
然后,您需要调整它的大小以适应200x200,您可以尝试这种方法https://stackoverflow.com/a/537697/660907
https://stackoverflow.com/questions/26754775
复制相似问题