我的第一个有点意思的IOS工程 - 图片局部放大

来源:互联网 发布:知乎是什么软件 编辑:程序博客网 时间:2024/04/29 11:18

是一个sva工程,现在可以把图片局部放大4倍,后期有时间了加上新的控制和设置部分

//

//  ViewController.h

//  sva_10_7_1

//

//  Created by 李吉 on 15/9/4.

//  Copyright (c) 2015李吉. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController :UIViewController

{

    NSArray *aryStrImageName;

    NSInteger iImageNumber;

    UIGestureRecognizer *gr;

}

@property (strong,nonatomic) IBOutletUIImageView *ivMain;

@property (strong,nonatomic) IBOutletUIImageView *ivSubMain;

- (IBAction)onBtnLastImageTouchUp:(id)sender;

- (IBAction)onBtnNextImageTouchUp:(UIButton *)sender;

- (IBAction)onBtnVaguerImageTouchUp:(UIButton *)sender;

- (IBAction)onBtnObviouserTouchUp:(UIButton *)sender;

@end

//

//  ViewController.m

//  sva_10_7_1

//

//  Created by 李吉 on 15/9/4.

//  Copyright (c) 2015李吉. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    @try {

        self.view.backgroundColor=[UIColorcolorWithRed:0.2green:0.2blue:0.2alpha:1];

        self.ivMain.highlighted=YES;

        aryStrImageName=[NSArrayarrayWithObjects:@"lijiang.jpg",@"qiao.jpg",@"shuangta.jpg",@"shui.jpg",@"xiangbi.jpg",nil];

        iImageNumber=0;

        if(aryStrImageName.count>0)

            _ivMain.image=[UIImageimageNamed:aryStrImageName[0]];

        self.ivMain.userInteractionEnabled=YES;

        UITapGestureRecognizer *st=[[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(ivMainTap:)];

        [self.ivMainaddGestureRecognizer:st];

        _ivSubMain.contentMode=UIViewContentModeScaleToFill;

        _ivMain.contentMode=UIViewContentModeScaleToFill;

        _ivSubMain.image=nil;

    }

    @catch (NSException *exception) {

        NSLog(@"%@\n",exception.reason);

    }

    @finally {

    }

}



//将触摸点的图像放大,显示在ivSubMain控件上

-(IBAction)ivMainTap:(UIGestureRecognizer *)sender

{

    bool bIsTouchValid=YES;

    NSUInteger uiWidthInImage=(_ivMain.image.size.width/_ivMain.frame.size.width)*50;

    NSUInteger uiHeightInImage=(_ivMain.image.size.height/_ivMain.frame.size.height)*50;

    CGPoint ptTouch = [senderlocationInView:self.ivMain];

    CGFloat fX=(ptTouch.x-25)*(_ivMain.image.size.width/_ivMain.frame.size.width);

    CGFloat fY=(ptTouch.y-25)*(_ivMain.image.size.height/_ivMain.frame.size.height);

    if(ptTouch.x-25<0||ptTouch.x+25>300)

        bIsTouchValid=NO;

    if(ptTouch.y-25<0||ptTouch.y+25>300)

        bIsTouchValid=NO;

    if(!bIsTouchValid)

        self.ivSubMain.image=nil;

    else

    {

        CGImageRef imgrfOrigin=_ivMain.image.CGImage;

        CGImageRef imgrfDest=CGImageCreateWithImageInRect(imgrfOrigin,CGRectMake(fX, fY, uiWidthInImage, uiHeightInImage));

        _ivSubMain.contentMode=UIViewContentModeScaleToFill;

        _ivSubMain.image=[UIImageimageWithCGImage:imgrfDest];

    }

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)onBtnLastImageTouchUp:(id)sender {

    if(--iImageNumber<0)

        iImageNumber=aryStrImageName.count-1;

    self.ivMain.image=[UIImageimageNamed:aryStrImageName[iImageNumber]];

    _ivSubMain.image=nil;

}


- (IBAction)onBtnNextImageTouchUp:(UIButton *)sender {

    iImageNumber=++iImageNumber%aryStrImageName.count;

    self.ivMain.image=[UIImageimageNamed:aryStrImageName[iImageNumber]];

    _ivSubMain.image=nil;

}


- (IBAction)onBtnVaguerImageTouchUp:(UIButton *)sender {

    _ivMain.alpha-=0.1;

    if(_ivMain.alpha<0)

        _ivMain.alpha=0;

    _ivSubMain.alpha=_ivMain.alpha;

    

}


- (IBAction)onBtnObviouserTouchUp:(UIButton *)sender {

    _ivMain.alpha+=0.1;

    if(_ivMain.alpha>1)

        _ivMain.alpha=1;

    _ivSubMain.alpha=_ivMain.alpha;

}

@end

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7531" systemVersion="14D2134" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="ufC-wZ-h7g">
            <objects>
                <viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
                        <viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <subviews>
                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="mKW-vM-RAo">
                                <rect key="frame" x="87" y="447" width="200" height="200"/>
                            </imageView>
                            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="jA4-7F-tDK">
                                <rect key="frame" x="26" y="50" width="50" height="30"/>
                                <color key="backgroundColor" red="0.0" green="0.0" blue="0.46788990825688076" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" type="system" pointSize="15"/>
                                <state key="normal" title="上一张">
                                    <color key="titleColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                                <connections>
                                    <action selector="onBtnLastImageTouchUp:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="b3q-WK-1OX"/>
                                </connections>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7FY-Zf-9Bn">
                                <rect key="frame" x="116" y="50" width="50" height="30"/>
                                <color key="backgroundColor" red="0.0" green="0.0" blue="0.46788990825688076" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" type="system" pointSize="15"/>
                                <state key="normal" title="下一张">
                                    <color key="titleColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                                <connections>
                                    <action selector="onBtnNextImageTouchUp:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="FqY-Iy-fbt"/>
                                </connections>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oFa-Jf-LMp">
                                <rect key="frame" x="206" y="50" width="50" height="30"/>
                                <color key="backgroundColor" red="0.0" green="0.0" blue="0.46788990825688076" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" type="system" pointSize="15"/>
                                <state key="normal" title="变  淡">
                                    <color key="titleColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                                <connections>
                                    <action selector="onBtnVaguerImageTouchUp:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="t8k-vI-Nak"/>
                                </connections>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ffg-bT-hun">
                                <rect key="frame" x="296" y="50" width="50" height="30"/>
                                <color key="backgroundColor" red="0.0" green="0.0" blue="0.46788990825688076" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" type="system" pointSize="15"/>
                                <state key="normal" title="增  强">
                                    <color key="titleColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                                <connections>
                                    <action selector="onBtnObviouserTouchUp:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="SSb-UJ-LRf"/>
                                </connections>
                            </button>
                            <imageView contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="xiangbi.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="PeG-Xu-JJG">
                                <rect key="frame" x="37" y="119" width="300" height="300"/>
                            </imageView>
                        </subviews>
                        <color key="backgroundColor" red="0.0" green="0.12232415902140673" blue="0.11926605504587151" alpha="1" colorSpace="calibratedRGB"/>
                    </view>
                    <simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
                    <connections>
                        <outlet property="ivMain" destination="PeG-Xu-JJG" id="7yj-Bj-nxg"/>
                        <outlet property="ivSubMain" destination="mKW-vM-RAo" id="PgT-FZ-oC9"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
            </objects>
        </scene>
    </scenes>
    <resources>
        <image name="xiangbi.jpg" width="1024" height="768"/>
    </resources>
</document>





0 0
原创粉丝点击