1

我在stackoverflow上看到了针对同一问题的不同帖子,但没有一个对我有用,因此我发布了我的查询

我正在开发一个应用程序,我从图像库中挑选图像并将其放置在我的UIImageView实例上

drawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"abc.png"]];
[drawImage setContentMode:UIViewContentModeScaleToFill];
drawImage.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
drawImage.clipsToBounds = YES;

[drawImage setFrame:self.view.frame];
NSLog(@"%@",NSStringFromCGSize(drawImage.frame.size));
[self.view addSubview:drawImage];

这是图片库的代码

imagePickerObj.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerObj.allowsEditing=YES;

当我从图库中选择图像时,我正在使用以下代码

UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
NSLog(@" B4 crop Image size %@",NSStringFromCGSize(img.size));
NSLog(@" B4 crop DrawImage size %@",NSStringFromCGSize(drawImage.frame.size));

UIGraphicsBeginImageContext(drawImage.frame.size);
[img drawInRect:CGRectMake(0,0,drawImage.frame.size.width,drawImage.frame.size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

NSLog(@"After crop Image size %@",NSStringFromCGSize(newImage.size));
UIGraphicsEndImageContext();


// drawImage.clipsToBounds = YES;
drawImage.image = newImage;

问题是,无论我选择什么图像,它似乎总是从我添加的上述代码中有点拉伸,因为UIGraphicsBeginImageContext它减小了图像的宽度和高度,但它仍然显得拉伸。

我什至尝试将图像视图的内容模式更改为AspectFillAspectFit但可惜没有运气。

你能指导我在哪里犯错吗?

4

0 回答 0