CaptureView permette di clonare, sotto forma di immagine, una qualsiasi vista (UIView o UIImageView) in modo rapido. Quello che si ottiene è un oggetto CaptureView che può essere utilizzato come una normale UIView (quindi visualizzato). Catturare lo “schermo”, o una sua porzione, può essere utile in molti casi. Una volta ottenuta la nuova “immagine”, questa potrà essere manipolata senza preoccparsi del suo contenuto.
Spesso si hanno più view innestate ed effetuare effetti o trasformazioni sulla view madre che le contiene tutte non è sempre una soluzione ottimale.
Inoltre esiste un baco, o strano comportamento, tale che se effettuiamo un’animazione su una view “contenitore” (anche con la proprietà autoresizesSubviews impostata a true), le subview non verranno animate correttamente, ma si posizioneranno immediatamente al punto d’arrivo.
In aggiunta non è certo performate ruotare o inclinare una “matrioska” di view.
Più conveniente, invece, è eseguire uno snapshot e alterare solo quello.
Un esempio d’uso è:
1 2 3 4 5 6 7 | #import "CaptureView.h" // ... CaptureView *cloneView = [[CaptureView alloc] initViewView: self.view ]; [self.view addSubview:cloneView]; [cloneView setTransform:CGAffineTransformMakeScale(0.5, 0.5)]; |
I codici sorgenti sono qui sotto:
CaptureView.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // // CaptureView.h // // Created by Giovambattista Fazioli on 02/12/10. // Copyright 2010 Saidmade srl. All rights reserved. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> #import <QuartzCore/CALayer.h> @interface CaptureView : UIView { @private UIImage *_imageCapture; } @property(nonatomic, retain) UIImage *imageCapture; // Init - (id)initWithView:(UIView *)view; @end |
CaptureView.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | // // CaptureView.m // // Created by Giovambattista Fazioli on 02/12/10. // Copyright 2010 Saidmade srl. All rights reserved. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // #import "CaptureView.h" // Private @interface CaptureView (/* Private */) - (void)settingImageFromView:(UIView *)view; @end // Public @implementation CaptureView @synthesize imageCapture = _imageCapture; // Standard - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // Initialization code. } return self; } // Init - (id)initWithView:(UIView *)view { if ((self = [super initWithFrame:[view frame]])) { // Initialization code. [self settingImageFromView:view]; } return self; } /*! @method imageFromView:view @abstract Esegue una Grab di una intera view @discussion Imposta l'immagine catturata eseguendo una copia dell'intera view */ - (void)settingImageFromView:(UIView *)view { CGRect rect = [view bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [view.layer renderInContext:context]; _imageCapture = UIGraphicsGetImageFromCurrentImageContext(); [_imageCapture retain]; UIGraphicsEndImageContext(); } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code. CGPoint accPoint = CGPointMake(0,0); [_imageCapture drawAtPoint:accPoint]; } - (void)dealloc { [_imageCapture release]; [super dealloc]; } @end |








4
Non ci sono commenti per questo Post
Lascia un commento