我正在添加一个新的子视图
self.editView = [[EditView alloc] initWithFrame:CGRectMake(0,0,320,296) WithImageView:self.mainImageView.image];
self.mainImageView.hidden=YES;
[self.view addSubview:self.editView];
新观点是:
- (id)initWithFrame:(CGRect)frame WithImageView: (UIImage *) image
{
self = [super initWithFrame:frame];
if (self) {
self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
self.pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
self.panRecognizer.cancelsTouchesInView = NO;
self.panRecognizer.delegate = self;
[self.frameView addGestureRecognizer:self.panRecognizer];
self.rotationRecognizer.cancelsTouchesInView = NO;
self.rotationRecognizer.delegate = self;
[self.frameView addGestureRecognizer:self.rotationRecognizer];
self.pinchRecognizer.cancelsTouchesInView = NO;
self.pinchRecognizer.delegate = self;
[self.frameView addGestureRecognizer:self.pinchRecognizer];
self.tapRecognizer.numberOfTapsRequired = 2;
[self.frameView addGestureRecognizer:self.tapRecognizer];
[self updateCropRect];
[self reset:NO];
self.retainedImage = image;
self.sourceImage = image;
self.imageView.image = self.previewImage;
if(self.previewImage != self.sourceImage) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGImageRef hiresCGImage = NULL;
CGFloat aspect = self.sourceImage.size.height/self.sourceImage.size.width;
CGSize size;
if(aspect >= 1.0) { //square or portrait
size = CGSizeMake(kMaxUIImageSize*aspect,kMaxUIImageSize);
} else { // landscape
size = CGSizeMake(kMaxUIImageSize,kMaxUIImageSize*aspect);
}
hiresCGImage = [self newScaledImage:self.sourceImage.CGImage withOrientation:self.sourceImage.imageOrientation toSize:size withQuality:kCGInterpolationDefault];
// dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithCGImage:hiresCGImage scale:1.0 orientation:UIImageOrientationUp];
CGImageRelease(hiresCGImage);
// });
});
}
}
return self;
}
还有图像的自定义设置器方法。
- (UIImage *)previewImage
{
if(_previewImage == nil && _sourceImage != nil) {
if(self.sourceImage.size.height > kMaxUIImageSize || self.sourceImage.size.width > kMaxUIImageSize) {
CGFloat aspect = self.sourceImage.size.height/self.sourceImage.size.width;
CGSize size;
if(aspect >= 1.0) { //square or portrait
size = CGSizeMake(kPreviewImageSize,kPreviewImageSize*aspect);
} else { // landscape
size = CGSizeMake(kPreviewImageSize,kPreviewImageSize*aspect);
}
_previewImage = [self scaledImage:self.sourceImage toSize:size withQuality:kCGInterpolationLow];
} else {
//self.previewImage = [self.sourceImage retain];
_previewImage = self.retainedImage;
}
}
return _previewImage;
}
我无法在子视图中加载图像。我知道属性不会在初始化时设置。但是我该如何设置它们。我尝试使用另一个属性retainedImage,并在其setter方法中将其分配给预览图像,但不起作用。
这是在这里找到的自定义库