IOS手指控制图片的缩放

简介: IOS手指控制图片的缩放
//
//  MoveScaleImageController.h
//  MoveScaleImage
//
//  Created by  on 12-4-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MoveScaleImageView.h"
@interface MoveScaleImageController : UIViewController<UIScrollViewDelegate>{
    UIScrollView *myScrollView;
    UIImageView *myImageView;
}
@property(retain,nonatomic)UIScrollView *myScrollView;
@property(retain,nonatomic)UIImageView *myImageView;
@end
//
//  MoveScaleImageController.m
//  MoveScaleImage
//
//  Created by  on 12-4-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "MoveScaleImageController.h"
@interface MoveScaleImageController ()
@end
@implementation MoveScaleImageController
@synthesize myScrollView;
@synthesize myImageView;
-(void)dealloc{
    [myScrollView release];
    [myImageView release];
    [super dealloc];
}
-(void)loadView{
    [super loadView];
    self.view.backgroundColor = [UIColor lightGrayColor];
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 50)];
//    [btn setFrame:CGRectMake(110, 200, 100, 40)];
    [btn setBackgroundColor:[UIColor whiteColor]];
    [btn setTitle:@"点击查看图片" forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn release];
    //下面是我要剪切区域的覆盖层
//    if(self.centerOverLayView==nil)
//    {
//        UIView *centerView=[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 210)];
//        self.centerOverLayView=centerView;
//        [centerView release];
//    }
//    self.centerOverLayView.backgroundColor=[UIColor clearColor];
//    self.centerOverLayView.layer.borderColor=[UIColor orangeColor].CGColor;
//    self.centerOverLayView.layer.borderWidth=2.0;
//    [self.view addSubview:self.centerOverLayView];
}
-(void)clickEvent:(id)sender{
    NSLog(@"***********clickeventad");
    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    if(self.myScrollView==nil)
    {
        UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        self.myScrollView=scrollView;
        [scrollView release];
    }
    self.myScrollView.backgroundColor=[UIColor blueColor];
    self.myScrollView.delegate=self;
    self.myScrollView.multipleTouchEnabled=YES;
    self.myScrollView.minimumZoomScale=1.0;
    self.myScrollView.maximumZoomScale=10.0;
    [self.view addSubview:self.myScrollView];
    UIImage *_image = [UIImage imageNamed:@"image.jpg"];
    CGFloat imageView_X = (_image.size.width > self.view.frame.size.width) ? self.view.frame.size.width : _image.size.width;
    CGFloat imageView_Y;
    CGFloat origin;
    if(_image.size.width > self.view.frame.size.width){
        origin = self.view.frame.size.width/_image.size.width;
        imageView_Y = _image.size.height*origin;
    }
    myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-imageView_X)/2, (self.view.frame.size.height-imageView_Y)/2, imageView_X, imageView_Y)];
    if(self.myImageView==nil)
    {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        self.myImageView=imageView;
        [imageView release];
    }
//    [myImageView setImage:_image];
    UIImage *originImage=[[UIImage alloc]initWithCGImage:_image.CGImage];
    [myImageView setImage:originImage];
//    [myImageView setFrame:CGRectMake(0, 0, _image.size.width, _image.size.height)];
    [self.myScrollView addSubview:self.myImageView];
    UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    [closeBtn setBackgroundColor:[UIColor redColor]];
    [closeBtn setAlpha:0.5];
    [closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
    [closeBtn release];
//    UIView *backView = [[UIView alloc] initWithFrame:CGRectInset(self.view.frame, 5, 5)];
//    backView.alpha = 0.5;
//    backView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:backView];
//    
//    UIImage* image=[UIImage imageNamed:@"image.jpg"];
//    MoveScaleImageView*fileContent = [[MoveScaleImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];
//    [fileContent setImage:image];
//    
    [backView addSubview:fileContent];
//    [self.view addSubview:fileContent];
//    
//    [backView release];
//    [fileContent release];
}
-(void)closeEvent:(id)sender{
    [self.myImageView setHidden:YES];
    [self.myScrollView setHidden:YES];
}
#pragma mark UIScrollView delegate methods
//实现图片的缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    NSLog(@"**************viewForZoomingInScrollView");
    return self.myImageView;
}
//实现图片在素芳过程中居中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    self.myImageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
相关文章
|
2月前
|
JSON JavaScript 安全
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
33 1
|
10月前
|
iOS开发
iOS TextView插入表情或者图片后字体变大或变小
iOS TextView插入表情或者图片后字体变大或变小
77 1
|
11月前
|
Android开发 iOS开发
iOS 替换WebView网页图片为本地图片
iOS 替换WebView网页图片为本地图片
222 0
|
2月前
|
存储 缓存 安全
基于iOS平台的高效图片缓存策略实现
【4月更文挑战第22天】 在移动应用开发中,图片资源的加载与缓存是影响用户体验的重要因素之一。尤其对于iOS平台,由于设备存储空间的限制以及用户对流畅性的高要求,设计一种合理的图片缓存策略显得尤为关键。本文将探讨在iOS环境下,如何通过使用先进的图片缓存技术,包括内存缓存、磁盘缓存以及网络请求的优化,来提高应用的性能和响应速度。我们将重点分析多级缓存机制的设计与实现,并对可能出现的问题及其解决方案进行讨论。
|
2月前
|
存储 缓存 算法
实现iOS平台的高效图片缓存策略
【4月更文挑战第22天】在移动应用开发中,图片资源的处理是影响用户体验的重要因素之一。特别是对于图像资源密集型的iOS应用,如何有效地缓存图片以减少内存占用和提升加载速度,是开发者们面临的关键挑战。本文将探讨一种针对iOS平台的图片缓存策略,该策略通过结合内存缓存与磁盘缓存的机制,并采用先进的图片解码和异步加载技术,旨在实现快速加载的同时,保持应用的内存效率。
|
2月前
|
存储 缓存 编解码
实现iOS平台的高效图片缓存策略
【4月更文挑战第23天】在移动应用开发领域,尤其是图像处理密集型的iOS应用中,高效的图片缓存策略对于提升用户体验和节省系统资源至关重要。本文将探讨一种针对iOS平台设计的图片缓存方案,该方案通过结合内存缓存与磁盘缓存的多层次结构,旨在优化图片加载性能并降低内存占用。我们将深入分析其设计理念、核心组件以及在实际场景中的应用效果,同时对比其他常见缓存技术的优势与局限。
|
2月前
|
存储 Web App开发 Android开发
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
63 1
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
|
2月前
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
22 0
|
2月前
|
存储 缓存 iOS开发
实现iOS平台的高效图片缓存策略
【4月更文挑战第4天】在移动应用开发中,图片资源的加载与缓存是影响用户体验的关键因素之一。尤其对于iOS平台,由于设备存储和内存资源的限制,设计一个高效的图片缓存机制尤为重要。本文将深入探讨在iOS环境下,如何通过技术手段实现图片的高效加载与缓存,包括内存缓存、磁盘缓存以及网络层面的优化,旨在为用户提供流畅且稳定的图片浏览体验。
|
2月前
|
存储 缓存 监控
实现iOS平台的高效图片缓存策略
【4月更文挑战第18天】在移动应用开发中,图片资源的加载与缓存是影响用户体验的重要因素之一。特别是对于iOS平台,合理设计图片缓存策略不仅能够提高应用的响应速度,还能降低内存消耗和网络流量。本文将探讨一种针对iOS环境的图片缓存方案,该方案通过多级缓存机制、内存管理和磁盘存储策略相结合,旨在提升图片加载效率并优化性能。