Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is a necessary consequence of resizing your image that it will take some amount of time. How much depends on the device, the resolution of the asset and the format of the asset. But you don't have any control over that. But you do have control over where the resizing takes place. I suspect that right now you are resizing the image in your main thread, which will cause the UI to grind to a halt while you are doing the resizing. Do enough images, and your app will appear hung for long enough that the user will just go off and do something else (perhaps check out competing apps in the App Store).</p> <p>What you should be doing is performing the resizing off the main thread. With iOS 4 and later, this has become much simpler because you can use Grand Central Dispatch to do the resizing. You can take your original block of code from above and wrap it in a block like this:</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ ALAssetRepresentation *rep = [[dict objectForKey:@"assetObject"] defaultRepresentation]; CGImageRef iref = [rep fullResolutionImage]; if (iref) { CGRect screenBounds = [[UIScreen mainScreen] bounds]; __block UIImage *previewImage; __block UIImage *largeImage; if([rep orientation] == ALAssetOrientationUp) //landscape image { largeImage = [[UIImage imageWithCGImage:iref] scaledToWidth:screenBounds.size.width]; previewImage = [[UIImage imageWithCGImage:iref] scaledToWidth:300]; } else // portrait image { previewImage = [[[UIImage imageWithCGImage:iref] scaledToHeight:300] imageRotatedByDegrees:90]; largeImage = [[[UIImage imageWithCGImage:iref] scaledToHeight:screenBounds.size.height] imageRotatedByDegrees:90]; } dispatch_async(dispatch_get_main_queue(), ^{ // do what ever you need to do in the main thread here once your image is resized. // this is going to be things like setting the UIImageViews to show your new images // or adding new views to your view hierarchy }); } }); </code></pre> <p>You'll have to think about things a little differently this way. For example, you've now broken up what used to be a single step into multiple steps now. Code that was running after this will end up running before the image resize is complete or before you actually do anything with the images, so you need to make sure that you didn't have any dependencies on those images or you'll likely crash.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload