拼图

来源:互联网 发布:android文件管理器源码 编辑:程序博客网 时间:2024/04/28 04:26

拼图主要用到的知识点 :

 根据范围截图

 CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);

 得到新的图片

 UIImage *elementImage = [UIImage imageWithCGImage:imageRef];

 把图片转化成NSData数据

 NSData *imageData = UIImageJPEGRepresentation(elementImage, 1);

具体实现的代码如下:

#import <UIKit/UIKit.h>


@protocol imageDelegate <NSObject>

@optional

-(void)moveDirection:(UIImageView *)imageview;

@end


@interface ImageView : UIImageView

{

    id delegate;

}


@property (nonatomic,retain) id delegate;


@end



#import "ImageView.h"

#import <QuartzCore/QuartzCore.h>


@implementation ImageView

@synthesize delegate;




- (id) initWithImage:(UIImage *)image

{

    if (self = [super initWithImage:image]) {

        [self setUserInteractionEnabled:YES];

        [self setMultipleTouchEnabled:YES];

        self.layer.borderWidth = 1;

        self.layer.borderColor = UIColor.grayColor.CGColor;

        

    }

    return self;

}


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    if([delegate respondsToSelector:@selector(moveDirection:)])

    {

        [delegate moveDirection:self];

   }

}




@end


#import <UIKit/UIKit.h>

#import "ImageView.h"

@interface ViewController : UIViewController<imageDelegate>



-(void)upchange:(UIImageView *)image;

-(void)leftchange:(UIImageView *)image;

-(void)rightchange:(UIImageView *)image;

-(void)downchange:(UIImageView *)image;

-(NSDictionary *)Separate:(UIImage *)image ByX:(int)x andY:(int)y;


@end



//

//  ViewController.m

//  ui-no-6拼图练习

//

//  Created by lchl-mac on 15/7/24.

//  Copyright (c) 2015 lchl-mac. All rights reserved.

//


#import "ViewController.h"

#import "ImageView.h"

#define DISTANCE 125

@interface ViewController ()

{

    UIImageView *referenceImage;

    ImageView *imageview;

    NSString *imageString;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor blackColor];

    NSLog(@"%f,%f",self.view.frame.origin.x,self.view.frame.origin.y);

    NSDictionary *dict = [[NSDictionary alloc]init];

    dict = [self Separate:[UIImage imageNamed:@"2.jpg"] ByX:3 andY:3];

    referenceImage = [[UIImageView alloc]initWithFrame:CGRectMake(250, 250+120, DISTANCE, DISTANCE)];

    referenceImage.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:referenceImage];

    for(int i =0;i<3;i++)

    {

        for (int j = 0; j<3; j++)

        {

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

            NSString *Documents = [paths lastObject];

            NSString *filepath = [Documents stringByAppendingPathComponent:[NSString stringWithFormat:@"win_%d_%d.jpg",i,j]];

            UIImage *image = [UIImage imageWithContentsOfFile:filepath];

            imageview = [[ImageView alloc] initWithImage:image];

            imageview.tag = i*2+j;

            if(i == 1)

            {

                imageview.tag = i*2+j+1;

            }

            if(i==2)

            {

                imageview.tag = i*2+j+2;

            }

            imageview.delegate = self;

            imageview.frame = CGRectMake(DISTANCE*i, DISTANCE*j+120, DISTANCE, DISTANCE);

            [self.view addSubview:imageview];

        }

    }

    UIImageView *imageViewOne = (UIImageView *)[self.view viewWithTag:8];

    imageViewOne.hidden = YES;

}

-(void)moveDirection:(UIImageView *)imageView

{

    CGFloat wid = imageView.frame.origin.x;

    CGFloat hig = imageView.frame.origin.y;

    

    if(referenceImage.frame.origin.x==wid+DISTANCE&&referenceImage.frame.origin.y==hig)

    {

        NSLog(@"right");

        [self rightchange:imageView];

    }

    if(referenceImage.frame.origin.x==wid-DISTANCE&&referenceImage.frame.origin.y==hig)

    {

        NSLog(@"left");

        [self leftchange:imageView];

    }

    if(referenceImage.frame.origin.x==wid&&referenceImage.frame.origin.y==hig-DISTANCE)

    {

        NSLog(@"down");

        [self downchange:imageView];

        

    }

    if(referenceImage.frame.origin.x==wid&&referenceImage.frame.origin.y==hig+DISTANCE)

    {

        NSLog(@"up");

        [self upchange:imageView];

    }

}

-(void)upchange:(UIImageView *)image

{

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3f];

    image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y+DISTANCE, DISTANCE, DISTANCE);

    referenceImage.frame = CGRectMake(image.frame.origin.x,image.frame.origin.y-DISTANCE , DISTANCE, DISTANCE);

}

-(void)leftchange:(UIImageView *)image

{

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3f];

    image.frame = CGRectMake(image.frame.origin.x-DISTANCE, image.frame.origin.y, DISTANCE, DISTANCE);

    referenceImage.frame = CGRectMake(image.frame.origin.x+DISTANCE,image.frame.origin.y, DISTANCE, DISTANCE);

}

-(void)rightchange:(UIImageView *)image

{

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3f];

    image.frame = CGRectMake(image.frame.origin.x+DISTANCE, image.frame.origin.y, DISTANCE, DISTANCE);

    referenceImage.frame = CGRectMake(image.frame.origin.x-DISTANCE,image.frame.origin.y, DISTANCE, DISTANCE);

}

-(void)downchange:(UIImageView *)image

    {

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3f];

    image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y-DISTANCE, DISTANCE, DISTANCE);

    referenceImage.frame = CGRectMake(image.frame.origin.x,image.frame.origin.y+DISTANCE , DISTANCE, DISTANCE);

    }

- (NSDictionary *)Separate:(UIImage *)image ByX:(int)x andY:(int)y

{

    float xLength = image.size.width*1.0/x;

    float yLength = image.size.height*1.0/y;

    NSMutableDictionary *mutableImageDictionary = [NSMutableDictionary dictionary];

    NSString *prefixName = @"win";

    for (int i = 0; i < x;i ++) {

        for (int j = 0; j < y; j ++) {

            CGRect rect = CGRectMake(xLength * j, yLength * i, xLength, yLength);

            CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);

            UIImage *elementImage = [UIImage imageWithCGImage:imageRef];

            UIImageView *imageView = [[UIImageView alloc]initWithImage:elementImage];

            imageView.frame = rect;

            imageString = [NSString stringWithFormat:@"%@_%d_%d.jpg",prefixName,i,j];

            [mutableImageDictionary setObject:imageView forKey:imageString];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

            NSString *Documents = [paths lastObject];

            NSString *imagePath = [Documents stringByAppendingPathComponent:imageString];

            NSData *imageData = UIImageJPEGRepresentation(elementImage, 1);

            [imageData writeToFile:imagePath atomically:YES];

            NSLog(@"%@",imagePath);

        }

    }

    NSDictionary *dictionary = mutableImageDictionary;

    NSLog(@"dictionary:%@",dictionary);

   

    return dictionary;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        // Return YES for supported orientations

        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

    }



@end


最终实现的效果图:






0 0
原创粉丝点击