我有一个UIImage,我从UIImagePickerController获取.当我从- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法中收到图像时,我将其直接放入UIImageView进行预览,并为用户提供保存或丢弃图像的选项.

当用户选择保存选项时,应用程序获取图像的png数据并将其保存到文档目录中.但是在2种可能的设备方向之一(仅景观)下发生的事情是图像被颠倒保存(更具体地说,从它应该的位置旋转180度).因此,当我在图库中加载图像时,它会显示为颠倒.
(参见图库中的左下角图片)

我已经解决了UIImage来自UIImage的UIImage中的原始图像数据没有旋转,而是将方向存储为对象的属性,仅在显示时应用.所以我试图使用我在网上找到的一些代码来旋转与UIImage相关联的CGImage.但它似乎对图像完全没有影响.我正在使用的代码如下:
- (void)rotateImage:(UIImage*)image byRadians:(CGFloat)rads
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(rads);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image you want to rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// Rotate …Run Code Online (Sandbox Code Playgroud) 我的类包含一个UIImage属性,我想通过访问它的任何外部客户端强制它作为"复制"属性.但是,当我尝试在我的自定义setter中执行复制时,我得到关于UIImage不支持copyWithZone的运行时错误.那么,确保遵循正确的所有权政策的好方法是什么?
// declared in the interface as:
@property (nonatomic, readonly, copy) UIImage *personImage;
// class implementation
- (void)setPersonImage:(UIImage *)newImage
{
if (newImage != personImage)
{
[personImage release];
// UIImage doesn't support copyWithZone
personImage = [newImage copy];
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有UIImage的UIImageView.我想将这些图片的副本分配给两个变量.根据用户正在做的事情,应该操纵图像.问题是,每个变量的图像都是一样的.我想,因为它们是通过引用传递的.
let image1 = imageView.image!
let image2 = imageView.image!
Run Code Online (Sandbox Code Playgroud)
如何获得此图像的两个单独副本?
谢谢!
编辑:
我想要实现的目标:只裁剪一张图像,保持另一张图像与原始图像一样.
let imageLeft = googleImageView.image!
let imageRef = CGImageCreateCopy(googleImageView.image!.CGImage)
let imageRight = UIImage(CGImage: imageRef!, scale: googleImageView.image!.scale, orientation: googleImageView.image!.imageOrientation)
if translation.x < 0 {
let scale = imageLeft.scale
let newRect = CGRectMake(0, 0, (imageLeft.size.width + translation.x) * scale, imageLeft.size.height * scale)
let imageRef = CGImageCreateWithImageInRect(imageLeft.CGImage, newRect)
if let croppedImage = imageRef {
googleImageView.image = UIImage(CGImage: croppedImage, scale: scale, orientation: imageLeft.imageOrientation)
}
}
print("left image: \(imageLeft) right image \(imageRight)")
Run Code Online (Sandbox Code Playgroud)
上面的代码打印到控制台: …