Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy an UIImage?

Tags:

swift

uiimage

I have an UIImageView with an UIImage. I want to assign a copy of these picture to two variables. Based on what the user is doing the image should be manipulated. The problem is, the image is the same in each variables. I guess, because they are passed by reference.

let image1 = imageView.image!
let image2 = imageView.image!

How do I get two separate copies of this image?

What I want to achieve: Just crop the one image, keep the other like the original.

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)")

The code above prints to the console:

left image: <UIImage: 0x7fd020dca070>, {111, 167} 
right image <UIImage: 0x7fd020dda430>, {111, 167}
left image: <UIImage: 0x7fd020df9ba0>, {110, 167} 
right image <UIImage: 0x7fd020d45670>, {110, 167}

... and so on. So, BOTH images gets a new size. Only the left Image should get cropped.

like image 284
heikomania Avatar asked Jun 30 '15 05:06

heikomania


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

How do I find my UIImage path?

if you already have the image i.e. have added the file to your resources, you can use this to get the file path; NSString *string = [[NSBundle mainBundle] pathForResource:@"IMAGE_FILE_NAME" ofType:@"jpg"]; // or ofType:@"png", etc. Save this answer.

How do you add a UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]]; My UIImage object is declared in . h file and allocated in init method, I need to set image in viewDidLoad method.

How do I add an image to UIImage?

With Interface Builder it's pretty easy to add and configure a UIImageView. The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project).


1 Answers

As @Aggressor suggested you can copy your image from imageView this way:

let newCgIm = CGImageCreateCopy(imageView.image?.CGImage)
let newImage = UIImage(CGImage: newCgIm, scale: imageView.image!.scale, orientation: imageView.image!.imageOrientation)
like image 159
Dharmesh Kheni Avatar answered Oct 23 '22 10:10

Dharmesh Kheni