gpt4 book ai didi

ios - 如何在 objective c 中缩放和滑动来自 webservise 的多个图像

转载 作者:行者123 更新时间:2023-11-29 00:16:34 26 4
gpt4 key购买 nike

1) 在我的应用程序中,我想缩放和收缩图像。在 xib 文件中,scrollview 中有一个 imageView。Page 控件用于滑动图像。对于单个图像,它会缩放,但对于多个图像(来自网络服务),它不会滑动和缩放。 在 setMultipleImages 方法中,图像不会缩放和滑动,而如果图像是单个图像,它会正确缩放。 2)在UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];行代码中,想用uiimageview替换uiimage。我该怎么做?

我的代码是:

-(void)setMultipleImages{

for (int i = 0; i < [imageMArray count]; i++){
NSString *logoName;

NSArray *imageNameArray = [[imageMArray objectAtIndex:i] componentsSeparatedByString:@"/"];

logoName = [[NSString alloc]initWithFormat:@"%@",[imageNameArray lastObject]];

if (![[AppDelegateHelper getInstance] fileExist:logoName])
{
NSString *imageURL = [[NSString alloc]initWithFormat:@"%@%@",[UrlBuilder getWebsiteUrl],[imageMArray objectAtIndex:i]];

[self startProcessing];

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];

[activityIndicator stopAnimating];
DebugLog(@"%f,%f",image.size.width,image.size.height);

NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [docDir objectAtIndex:0];
DebugLog(@"saving png");

NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@",cacheDirectory,[imageNameArray lastObject]];

NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];

[data1 writeToFile:pngFilePath atomically:YES];
}
}

[self loadImageData];
}

-(void)loadImageData
{
int X =0;
for (int s=0; s<imageMArray.count; s++) {
NSString *logoName;

NSArray *imageNameArray = [[imageMArray objectAtIndex:s] componentsSeparatedByString:@"/"];


logoName = [[NSString alloc]initWithFormat:@"%@",[imageNameArray lastObject]];


if([[AppDelegateHelper getInstance] loadImage:logoName] != nil)
{
// UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;

[imageView setImage:[[AppDelegateHelper getInstance] loadImage:logoName]];

imageView.contentMode = UIViewContentModeScaleAspectFit;

[scrollView addSubview: imageView];

X = X + imageView.frame.size.width;
}
else
{
//UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;

[imageView setImage:[UIImage imageNamed:@"NoImage.png"]];

imageView.contentMode = UIViewContentModeScaleAspectFit;

[scrollView addSubview: imageView];

X = X + imageView.frame.size.width;
}
}

self.pageControl.numberOfPages = [imageMArray count];

scrollView.contentSize = CGSizeMake(X,scrollView.frame.size.height);

lblCount.text = [[NSString alloc]initWithFormat:@"1 sur %lu",(unsigned long)imageMArray.count];
}

最佳答案

将所有图像添加到 imageview。然后将 pinchguesture 应用于 ImageView 以实现缩放效果。

捏手势:

UIPinchGestureRecognizer *imageviewguesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(ImageViewPinchGuesture:)];

为您的 ImageView 添加捏手势:

[YourImageView addGestureRecognizer:imageviewguesture];

制作你的选择器方法:

-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer{    
pinGuestureregonizer.view.transform = CGAffineTransformScale(pinGuestureregonizer.view.transform, pinGuestureregonizer.scale, pinGuestureregonizer.scale);
pinGuestureregonizer.scale = 1;
}

在你的 .h 文件中添加这一行

-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer;

在模拟器中检查效果按 Cmd+alt 然后按鼠标左键并拖动。

希望这会有所帮助。如果对您有帮助,请不要忘记采纳答案。

关于ios - 如何在 objective c 中缩放和滑动来自 webservise 的多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45189896/

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