Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 63219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:35:54+00:00 2026-05-10T18:35:54+00:00

Objective: take a UIImage, crop out a square in the middle, change size of

  • 0

Objective: take a UIImage, crop out a square in the middle, change size of square to 320×320 pixels, slice up the image into 16 80×80 images, save the 16 images in an array.

Here’s my code:

CGImageRef originalImage, resizedImage, finalImage, tmp; float imgWidth, imgHeight, diff; UIImage *squareImage, *playImage; NSMutableArray *tileImgArray; int r, c;  originalImage = [image CGImage];  imgWidth = image.size.width; imgHeight = image.size.height; diff = fabs(imgWidth - imgHeight);  if(imgWidth > imgHeight){     resizedImage = CGImageCreateWithImageInRect(originalImage, CGRectMake(floor(diff/2), 0, imgHeight, imgHeight)); }else{     resizedImage = CGImageCreateWithImageInRect(originalImage, CGRectMake(0, floor(diff/2), imgWidth, imgWidth)); } CGImageRelease(originalImage);  squareImage = [UIImage imageWithCGImage:resizedImage];       if(squareImage.size.width != squareImage.size.height){     NSLog(@'image cutout error!');     //*code to return to main menu of app, irrelevant here }else{     float newDim = squareImage.size.width;     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{         playImage = squareImage;     } }  finalImage = [playImage CGImage]; tileImgArray = [NSMutableArray arrayWithCapacity:0]; for(int i = 0; i < 16; i++){     r = i/4;     c = i%4;     //*     tmp = CGImageCreateWithImageInRect(finalImage, CGRectMake(c*tileSize, r*tileSize, tileSize, tileSize));     [tileImgArray addObject:[UIImage imageWithCGImage:tmp]]; } 

The code works correctly when the original (the variable image) has its smaller dimension either bigger or smaller than 320 pixels. When it’s exactly 320, the resulting 80×80 images are almost entirely black, 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 image both directly:

[UIImage imageWithCGImage:finalImage]; 

And indirectly:

[UIImage imageWithCGImage:CGImageCreateWithImageInRect(finalImage, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:35:54+00:00Added an answer on May 10, 2026 at 6:35 pm

    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(); } 

    Anyone has any clue WHY this is going on?

    P.S. Yes, if/else is no longer required here. Removing it before I knew it was going to work would be stupid, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to take an objective-c UIImage and make it all black, but still
I'm learning objective-c and keep bumping into the @ symbol. It is used in
My objective is to take directions from a user and eventually a text file
Is there anyway we can take input from command line in Objective-C, the way
In Objective-C, NSString has a method called stringByExpandingTildeInPath This method will take a string
I am not fluent in objective-c and thus have decided to take the easier
I'm new at objective-c and stuck with a problem. Is it possible to take
Is it possible in objective C that we can take the screen shot of
I'm still learning Objective C and iPhone development, so I'm willing to take instruction.
I am trying to figure out how to take a given NSString and convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.