iOS开发之循环引用常见场景

#头文件

/*

File: LargeImageDownsizingViewController.h

Abstract: The primary view controller for this project.

Version: 1.1

Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple

Inc. ("Apple") in consideration of your agreement to the following

terms, and your use, installation, modification or redistribution of

this Apple software constitutes acceptance of these terms.  If you do

not agree with these terms, please do not use, install, modify or

redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and

subject to these terms, Apple grants you a personal, non-exclusive

license, under Apple's copyrights in this original Apple software (the

"Apple Software"), to use, reproduce, modify and redistribute the Apple

Software, with or without modifications, in source and/or binary forms;

provided that if you redistribute the Apple Software in its entirety and

without modifications, you must retain this notice and the following

text and disclaimers in all such redistributions of the Apple Software.

Neither the name, trademarks, service marks or logos of Apple Inc. may

be used to endorse or promote products derived from the Apple Software

without specific prior written permission from Apple.  Except as

expressly stated in this notice, no other rights or licenses, express or

implied, are granted by Apple herein, including but not limited to any

patent rights that may be infringed by your derivative works or by other

works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.  APPLE

MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION

THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS

FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND

OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL

OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,

MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED

AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),

STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE

POSSIBILITY OF SUCH DAMAGE.

Copyright (C) 2014 Apple Inc. All Rights Reserved.

*/

#import

@classImageScrollView;

@interfaceLargeImageDownsizingViewController :UIViewController{

// The input image file

UIImage* sourceImage;

// output image file

UIImage* destImage;

// sub rect of the input image bounds that represents the

// maximum amount of pixel data to load into mem at one time.

CGRectsourceTile;

// sub rect of the output image that is proportionate to the

// size of the sourceTile.

CGRectdestTile;

// the ratio of the size of the input image to the output image.

floatimageScale;

// source image width and height

CGSizesourceResolution;

// total number of pixels in the source image

floatsourceTotalPixels;

// total number of megabytes of uncompressed pixel data in the source image.

floatsourceTotalMB;

// output image width and height

CGSizedestResolution;

// the temporary container used to hold the resulting output image pixel

// data, as it is being assembled.

CGContextRefdestContext;

// the number of pixels to overlap tiles as they are assembled.

floatsourceSeemOverlap;

// an image view to visualize the image as it is being pieced together

UIImageView* progressView;

// a scroll view to display the resulting downsized image

ImageScrollView* scrollView;

}

// destImage property is specifically thread safe (i.e. no 'nonatomic' attribute)

// because it is accessed off the main thread.

@property(retain)UIImage* destImage;

-(void)downsize:(id)arg;

-(void)updateScrollView:(id)arg;

-(void)initializeScrollView:(id)arg;

-(void)createImageFromContext;

@end

源文件

/*

File: LargeImageDownsizingViewController.m

Abstract: The primary view controller for this project.

Version: 1.1

Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple

Inc. ("Apple") in consideration of your agreement to the following

terms, and your use, installation, modification or redistribution of

this Apple software constitutes acceptance of these terms.  If you do

not agree with these terms, please do not use, install, modify or

redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and

subject to these terms, Apple grants you a personal, non-exclusive

license, under Apple's copyrights in this original Apple software (the

"Apple Software"), to use, reproduce, modify and redistribute the Apple

Software, with or without modifications, in source and/or binary forms;

provided that if you redistribute the Apple Software in its entirety and

without modifications, you must retain this notice and the following

text and disclaimers in all such redistributions of the Apple Software.

Neither the name, trademarks, service marks or logos of Apple Inc. may

be used to endorse or promote products derived from the Apple Software

without specific prior written permission from Apple.  Except as

expressly stated in this notice, no other rights or licenses, express or

implied, are granted by Apple herein, including but not limited to any

patent rights that may be infringed by your derivative works or by other

works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.  APPLE

MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION

THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS

FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND

OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL

OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,

MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED

AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),

STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE

POSSIBILITY OF SUCH DAMAGE.

Copyright (C) 2014 Apple Inc. All Rights Reserved.

*/

#import"LargeImageDownsizingViewController.h"

#import

#import"ImageScrollView.h"

/* Image Constants: for images, we define the resulting image

size and tile size in megabytes. This translates to an amount

of pixels. Keep in mind this is almost always significantly different

from the size of a file on disk for compressed formats such as png, or jpeg.

For an image to be displayed in iOS, it must first be uncompressed (decoded) from

disk. The approximate region of pixel data that is decoded from disk is defined by both,

the clipping rect set onto the current graphics context, and the content/image

offset relative to the current context.

To get the uncompressed file size of an image, use: Width x Height / pixelsPerMB, where

pixelsPerMB = 262144 pixels in a 32bit colospace (which iOS is optimized for).

Supported formats are: PNG, TIFF, JPEG. Unsupported formats: GIF, BMP, interlaced images.

*/

#define kImageFilename @"large_leaves_70mp.jpg"// 7033x10110 image, 271 MB uncompressed

/* The arguments to the downsizing routine are the resulting image size, and

"tile" size. And they are defined in terms of megabytes to simplify the correlation

between images and memory footprint available to your application.

The "tile" is the maximum amount of pixel data to load from the input image into

memory at one time. The size of the tile defines the number of iterations

required to piece together the resulting image.

Choose a resulting size for your image given both: the hardware profile of your

target devices, and the amount of memory taken by the rest of your application.

Maximizing the source image tile size will minimize the time required to complete

the downsize routine. Thus, performance must be balanced with resulting image quality.

Choosing appropriate resulting image size and tile size can be done, but is left as

an exercise to the developer. Note that the device type/version string

(e.g. "iPhone2,1" can be determined at runtime through use of the sysctlbyname function:

size_t size;

sysctlbyname("hw.machine", NULL, &size, NULL, 0);

char *machine = malloc(size);

sysctlbyname("hw.machine", machine, &size, NULL, 0);

NSString* _platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];

free(machine);

These constants are suggested initial values for iPad1, and iPhone 3GS */

#define IPAD1_IPHONE3GS

#ifdef IPAD1_IPHONE3GS

#  define kDestImageSizeMB60.0f// The resulting image will be (x)MB of uncompressed image data.

#  define kSourceImageTileSizeMB20.0f// The tile size will be (x)MB of uncompressed image data.

#endif

/* These constants are suggested initial values for iPad2, and iPhone 4 */

//#define IPAD2_IPHONE4

#ifdef IPAD2_IPHONE4

#  define kDestImageSizeMB120.0f// The resulting image will be (x)MB of uncompressed image data.

#  define kSourceImageTileSizeMB40.0f// The tile size will be (x)MB of uncompressed image data.

#endif

/* These constants are suggested initial values for iPhone3G, iPod2 and earlier devices */

//#define IPHONE3G_IPOD2_AND_EARLIER

#ifdef IPHONE3G_IPOD2_AND_EARLIER

#  define kDestImageSizeMB30.0f// The resulting image will be (x)MB of uncompressed image data.

#  define kSourceImageTileSizeMB10.0f// The tile size will be (x)MB of uncompressed image data.

#endif

/* Constants for all other iOS devices are left to be defined by the developer.

The purpose of this sample is to illustrate that device specific constants can

and should be created by you the developer, versus iterating a complete list. */

#define bytesPerMB1048576.0f

#define bytesPerPixel4.0f

#define pixelsPerMB ( bytesPerMB / bytesPerPixel )// 262144 pixels, for 4 bytes per pixel.

#define destTotalPixels kDestImageSizeMB * pixelsPerMB

#define tileTotalPixels kSourceImageTileSizeMB * pixelsPerMB

#define destSeemOverlap2.0f// the numbers of pixels to overlap the seems where tiles meet.

@implementationLargeImageDownsizingViewController

@synthesizedestImage;

// release ownership

- (void)dealloc {

[destImagerelease];

[scrollViewrelease];

//--

[superdealloc];

}

- (void)didReceiveMemoryWarning {

// Releases the view if it doesn't have a superview.

[superdidReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

#pragma mark - View lifecycle

/*

-(void)loadView {

}*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

[superviewDidLoad];

//--

progressView= [[UIImageViewalloc]initWithFrame:self.view.bounds];

[self.viewaddSubview:progressView];

[progressViewrelease];

[NSThreaddetachNewThreadSelector:@selector(downsize:)toTarget:selfwithObject:nil];

}

- (void)viewDidUnload {

[superviewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations

return(interfaceOrientation ==UIInterfaceOrientationPortrait);

}

-(void)downsize:(id)arg {

// create an autorelease pool to catch calls to -autorelease.

NSAutoreleasePool* pool = [[NSAutoreleasePoolalloc]init];

// create an image from the image filename constant. Note this

// doesn't actually read any pixel information from disk, as that

// is actually done at draw time.

sourceImage= [[UIImagealloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:kImageFilenameofType:nil]];

if(sourceImage==nil)NSLog(@"input image not found!");

// get the width and height of the input image using

// core graphics image helper functions.

sourceResolution.width=CGImageGetWidth(sourceImage.CGImage);

sourceResolution.height=CGImageGetHeight(sourceImage.CGImage);

// use the width and height to calculate the total number of pixels

// in the input image.

sourceTotalPixels=sourceResolution.width*sourceResolution.height;

// calculate the number of MB that would be required to store

// this image uncompressed in memory.

sourceTotalMB=sourceTotalPixels/pixelsPerMB;

// determine the scale ratio to apply to the input image

// that results in an output image of the defined size.

// see kDestImageSizeMB, and how it relates to destTotalPixels.

imageScale=destTotalPixels/sourceTotalPixels;

// use the image scale to calcualte the output image width, height

destResolution.width= (int)(sourceResolution.width*imageScale);

destResolution.height= (int)(sourceResolution.height*imageScale);

// create an offscreen bitmap context that will hold the output image

// pixel data, as it becomes available by the downscaling routine.

// use the RGB colorspace as this is the colorspace iOS GPU is optimized for.

CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();

intbytesPerRow =bytesPerPixel*destResolution.width;

// allocate enough pixel data to hold the output image.

void* destBitmapData =malloc( bytesPerRow *destResolution.height);

if( destBitmapData ==NULL)NSLog(@"failed to allocate space for the output image!");

// create the output bitmap context

destContext=CGBitmapContextCreate( destBitmapData,destResolution.width,destResolution.height, 8, bytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast);

// remember CFTypes assign/check for NULL. NSObjects assign/check for nil.

if(destContext==NULL) {

free( destBitmapData );

NSLog(@"failed to create the output bitmap context!");

}

// release the color space object as its job is done

CGColorSpaceRelease( colorSpace );

// flip the output graphics context so that it aligns with the

// cocoa style orientation of the input document. this is needed

// because we used cocoa's UIImage -imageNamed to open the input file.

CGContextTranslateCTM(destContext, 0.0f,destResolution.height);

CGContextScaleCTM(destContext, 1.0f, -1.0f );

// now define the size of the rectangle to be used for the

// incremental blits from the input image to the output image.

// we use a source tile width equal to the width of the source

// image due to the way that iOS retrieves image data from disk.

// iOS must decode an image from disk in full width 'bands', even

// if current graphics context is clipped to a subrect within that

// band. Therefore we fully utilize all of the pixel data that results

// from a decoding opertion by achnoring our tile size to the full

// width of the input image.

sourceTile.size.width=sourceResolution.width;

// the source tile height is dynamic. Since we specified the size

// of the source tile in MB, see how many rows of pixels high it

// can be given the input image width.

sourceTile.size.height= (int)(tileTotalPixels/sourceTile.size.width);

NSLog(@"source tile size: %f x %f",sourceTile.size.width,sourceTile.size.height);

sourceTile.origin.x= 0.0f;

// the output tile is the same proportions as the input tile, but

// scaled to image scale.

destTile.size.width=destResolution.width;

destTile.size.height=sourceTile.size.height*imageScale;

destTile.origin.x= 0.0f;

NSLog(@"dest tile size: %f x %f",destTile.size.width,destTile.size.height);

// the source seem overlap is proportionate to the destination seem overlap.

// this is the amount of pixels to overlap each tile as we assemble the ouput image.

sourceSeemOverlap= (int)( (destSeemOverlap/destResolution.height) *sourceResolution.height);

NSLog(@"dest seem overlap: %f, source seem overlap: %f",destSeemOverlap,sourceSeemOverlap);

CGImageRefsourceTileImageRef;

// calculate the number of read/write opertions required to assemble the

// output image.

intiterations = (int)(sourceResolution.height/sourceTile.size.height);

// if tile height doesn't divide the image height evenly, add another iteration

// to account for the remaining pixels.

intremainder = (int)sourceResolution.height% (int)sourceTile.size.height;

if( remainder ) iterations++;

// add seem overlaps to the tiles, but save the original tile height for y coordinate calculations.

floatsourceTileHeightMinusOverlap =sourceTile.size.height;

sourceTile.size.height+=sourceSeemOverlap;

destTile.size.height+=destSeemOverlap;

NSLog(@"beginning downsize. iterations: %d, tile height: %f, remainder height: %d", iterations,sourceTile.size.height,remainder );

for(inty = 0; y < iterations; ++y ) {

// create an autorelease pool to catch calls to -autorelease made within the downsize loop.

NSAutoreleasePool* pool2 = [[NSAutoreleasePoolalloc]init];

NSLog(@"iteration %d of %d",y+1,iterations);

sourceTile.origin.y= y * sourceTileHeightMinusOverlap +sourceSeemOverlap;

destTile.origin.y= (destResolution.height) - ( ( y + 1 ) * sourceTileHeightMinusOverlap *imageScale+destSeemOverlap);

// create a reference to the source image with its context clipped to the argument rect.

sourceTileImageRef =CGImageCreateWithImageInRect(sourceImage.CGImage,sourceTile);

// if this is the last tile, it's size may be smaller than the source tile height.

// adjust the dest tile size to account for that difference.

if( y == iterations - 1 && remainder ) {

floatdify =destTile.size.height;

destTile.size.height=CGImageGetHeight( sourceTileImageRef ) *imageScale;

dify -=destTile.size.height;

destTile.origin.y+= dify;

}

// read and write a tile sized portion of pixels from the input image to the output image.

CGContextDrawImage(destContext,destTile, sourceTileImageRef );

/* release the source tile portion pixel data. note,

releasing the sourceTileImageRef doesn't actually release the tile portion pixel

data that we just drew, but the call afterward does. */

CGImageRelease( sourceTileImageRef );

/* while CGImageCreateWithImageInRect lazily loads just the image data defined by the argument rect,

that data is finally decoded from disk to mem when CGContextDrawImage is called. sourceTileImageRef

maintains internally a reference to the original image, and that original image both, houses and

caches that portion of decoded mem. Thus the following call to release the source image. */

[sourceImagerelease];

// free all objects that were sent -autorelease within the scope of this loop.

[pool2drain];

// we reallocate the source image after the pool is drained since UIImage -imageNamed

// returns us an autoreleased object.

if( y < iterations - 1 ) {

sourceImage= [[UIImagealloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:kImageFilenameofType:nil]];

[selfperformSelectorOnMainThread:@selector(updateScrollView:)withObject:nilwaitUntilDone:YES];

}

}

NSLog(@"downsize complete.");

[selfperformSelectorOnMainThread:@selector(initializeScrollView:)withObject:nilwaitUntilDone:YES];

// free the context since its job is done. destImageRef retains the pixel data now.

CGContextRelease(destContext);

[pooldrain];

}

-(void)createImageFromContext {

// create a CGImage from the offscreen image context

CGImageRefdestImageRef =CGBitmapContextCreateImage(destContext);

if( destImageRef ==NULL)NSLog(@"destImageRef is null.");

// wrap a UIImage around the CGImage

self.destImage= [UIImageimageWithCGImage:destImageRefscale:1.0forientation:UIImageOrientationDownMirrored];

// release ownership of the CGImage, since destImage retains ownership of the object now.

CGImageRelease( destImageRef );

if(destImage==nil)NSLog(@"destImage is nil.");

}

-(void)updateScrollView:(id)arg {

[selfcreateImageFromContext];

// display the output image on the screen.

progressView.image=destImage;

}

-(void)initializeScrollView:(id)arg {

[progressViewremoveFromSuperview];

[selfcreateImageFromContext];

// create a scroll view to display the resulting image.

scrollView= [[ImageScrollViewalloc]initWithFrame:self.view.boundsimage:self.destImage];

[self.viewaddSubview:scrollView];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容