gpt4 book ai didi

ios - 在没有 Nib 的情况下在 subview 中加载图像

转载 作者:行者123 更新时间:2023-11-29 04:05:41 25 4
gpt4 key购买 nike

我正在添加一个新的 subview

 self.editView = [[EditView alloc] initWithFrame:CGRectMake(0,0,320,296) WithImageView:self.mainImageView.image];
self.mainImageView.hidden=YES;
[self.view addSubview:self.editView];

新 View 是:

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

}

我无法在 subview 中加载图像。我知道这些属性不会在 init 上设置。但我该如何设置它们。我尝试使用另一个属性retainedImage,并在其setter方法中将其分配给预览图像,但不起作用。

这是一个找到的自定义库 here

最佳答案

创建对象时,对象的属性将设置为 nil。如果您想将属性更改为其他值,可以在任何方法中设置它们。

您看不到该图像的可能原因有很多。

  1. self.imageView 为零
  2. self.imageView.image 为零
  3. self.imageView的大小为0
  4. self.imageView 被隐藏
  5. self.imageView 未附加到任何 View
  6. self.imageView 被其他 View 遮挡

从你的描述来看,很难判断到底是哪一个原因。

关于ios - 在没有 Nib 的情况下在 subview 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com