---
title: "Problem in slicing an UIImage on iPhone"  
description: "Problem in slicing an UIImage on iPhone"  
author: "Tarun Kumar"  
published: 2015-09-16  
updated: 2015-09-17  
canonical: https://www.mindstick.com/forum/33461/problem-in-slicing-an-uiimage-on-iphone  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Problem in slicing an UIImage on iPhone

Take a UIImage, [crop](https://answers.mindstick.com/qa/32258/under-whose-chairmanship-a-regional-project-on-climate-resilience-building-among-farmers-through-crop-residue-management-has-launched) out a square in the middle, change [size](https://www.mindstick.com/forum/159799/how-can-you-manage-the-size-of-mdf-and-ldf-files) of square to 320x320 pixels, [slice](https://answers.mindstick.com/qa/115838/how-to-play-and-enjoy-slice-master-a-fun-experience-worth-trying) up the [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) into 16 80x80 [images](https://www.mindstick.com/interview/1067/what-is-the-php-predefined-variable-that-tells-the-what-type-of-images-that-php-support), save the 16 images in an array.\
Here's my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):

```
CGImageRef myImage, sizeImg, finalImg, tmp;float widthImg, heightImg, diff;UIImage *squareImg, *playImg;NSMutableArray *arrayImgTitle;int r, c;myImage = [image CGImage];widthImg = image.size.width;heightImg = image.size.height;diff = fabs(widthImg - heightImg);if(widthImg > heightImg) {   sizeImg = CGImageCreateWithImageInRect(myImage,             CGRectMake(floor(diff/2), 0, heightImg, heightImg));} else {   sizeImg = CGImageCreateWithImageInRect(myImage, CGRectMake(0,             floor(diff/2), widthImg, widthImg));}CGImageRelease(myImage);squareImg = [UIImage imageWithCGImage:sizeImg];  if(squareImg.size.width != squareImg.size.height) {   NSLog(@"image cutout error!");   //*code to return to main menu of app, irrelevant here} else {  float newDim = squareImg.size.width;  if(newDim != 320.0) {     CGSize finalSize = CGSizeMake(320.0, 320.0);     UIGraphicsBeginImageContext(finalSize);     [squareImg drawInRect:CGRectMake(0, 0, finalSize.width,                           finalSize.height)];     playImg = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();  } else {     playImg = squareImg;  }}finalImg = [playImg CGImage];arrayImgTitle = [NSMutableArray arrayWithCapacity:0];for(int i = 0; i < 16; i++) {   r = i/4;   c = i%4;   tmp = CGImageCreateWithImageInRect(finalImg, CGRectMake(c*tileSize,         r*tileSize, tileSize, tileSize));   [arrayImgTitle addObject:[UIImage imageWithCGImage:tmp]];}
```

The code works correctly when the original (the [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) image) has its smaller [dimension](https://www.mindstick.com/articles/1508/dimension-and-metrics-in-google-analytics-api-using-asp-dot-net-mvc) either bigger or smaller than 320 pixels. When it's exactly 320, the resulting 80x80 images are almost entirely [black](https://yourviews.mindstick.com/view/83102/badi-elaichi-black-cardamom-health-benefits), some with a few pixels at the edges that may (I can't really tell) be from the original image.

I tested by displaying the [full](https://www.mindstick.com/articles/12893/experience-the-full-power-of-suitecrm) image both directly:

[UIImage imageWithCGImage:finalImg];

And indirectly:

[UIImage imageWithCGImage:CGImageCreateWithImageInRect(finalImg, CGRectMake(0, 0, 320, 320))];

In both cases, the display worked. The problems only arise when I attempt to slice out some part of the image.\

## Replies

### Reply by Anonymous User

After some more experimentation, I found the following solution (I still don't know why it didn't work as originally written, though.) But anyway, the slicing works after the resize code is put in place even when resizing is unnecessary:

```
if(newDim != 320.0){            CGSize finalSize = CGSizeMake(320.0, 320.0);            UIGraphicsBeginImageContext(finalSize);            [squareImage drawInRect:CGRectMake(0, 0, finalSize.width,                                    finalSize.height)];            playImage = UIGraphicsGetImageFromCurrentImageContext();            UIGraphicsEndImageContext();}else{            CGSize finalSize = CGSizeMake(320.0, 320.0);            UIGraphicsBeginImageContext(finalSize);            [squareImage drawInRect:CGRectMake(0, 0, finalSize.width,                                    finalSize.height)];            playImage = UIGraphicsGetImageFromCurrentImageContext();            UIGraphicsEndImageContext();}
```


---

Original Source: https://www.mindstick.com/forum/33461/problem-in-slicing-an-uiimage-on-iphone

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
