文件,文件夹问题

本文介绍了iOS开发中的多种实用技巧,包括获取Document文件夹路径、调用摄像头拍照、下载文件、实现设备震动效果、获取设备号等,并展示了Unity3D截屏方法及iPhone不同方式获取文件路径的技术细节。
摘要由CSDN通过智能技术生成

IOS 获取 Document 文件夹路径


NSFileManager *fileManager = [NSFileManager defaultManager]; 

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

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"]; 

IOS调取摄像头并拍照
<UIImagePickerControllerDelegate>


1、打开摄像头

- (IBAction)Open:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        NSArray *temp_MediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:picker.sourceType];

        picker.mediaTypes = temp_MediaTypes;

        picker.delegate = self;

        picker.allowsImageEditing = YES;

    }

    [self presentModalViewController:picker animated:YES];

    [picker release];

}

2.拍照回调


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

     BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;

   

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

    NSString *documentsDirectory = [paths objectAtIndex:0];

   

    if ([mediaType isEqualToString:@"public.image"]){

       

        UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

        NSLog(@"found an image");

       NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"temp.jpg"];

        NSLog(@"%@", imageFile);

        success = [fileManager fileExistsAtPath:imageFile];

        if(success) {

            success = [fileManager removeItemAtPath:imageFile error:&error];

        }

       

        imageView.image = image;

        [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];

         //SETIMAGE(image);

        //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);

    }

    else if([mediaType isEqualToString:@"public.movie"]){

        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

        NSLog(@"%@", videoURL);

        NSLog(@"found a video");

        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

    

        NSString *videoFile = [documentsDirectory stringByAppendingPathComponent:@"temp.mov"];

        NSLog(@"%@", videoFile);

        success = [fileManager fileExistsAtPath:videoFile];

        if(success) {

            success = [fileManager removeItemAtPath:videoFile error:&error];

        }

        [videoData writeToFile:videoFile atomically:YES];

        //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);

        //NSLog(videoURL);

    }

    [picker dismissModalViewControllerAnimated:YES];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

       [picker dismissModalViewControllerAnimated:YES];

}

IOS下载文件(同步下载)


NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";

    NSURL    *url = [NSURL URLWithString:urlAsString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSError *error = nil;

   NSData* data= [NSURLConnection sendSynchronousRequest:request returningResponse:nilerror:&error];

    /* 下载的数据 */

    if (data != nil){

        NSLog(@"下载成功");

        if ([data writeToFile:@"/Users/jilucky/UIWebViewDemo.zip" atomically:YES]) {

            NSLog(@"保存成功.");

        }

        else

        {

            NSLog(@"保存失败.");

        }

    } else {

        NSLog(@"%@", error);

    }

IOS震动效果
AudioToolBox.framework

#import "AudioToolbox/AudioToolbox.h"
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

IOS获取设备号


    UIDevice *device = [UIDevice currentDevice];//创建设备对象

    NSString *deviceUID = [[NSString alloc] initWithString:[device uniqueIdentifier]];

    UIAlertView *view = [[UIAlertView alloc]initWithTitle:nil message:deviceUID delegate:selfcancelButtonTitle:@"ok" otherButtonTitles: nil];

    [view show];

Unity 3D截屏

1.js脚本(添加到ARCamera上)


#pragma strict

function Start () {}

function Update () {}

function ScreenImage(){

Application.CaptureScreenshot("Screenshot.png");

}

2.添加button


   UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];      

    [button1 setFrame:CGRectMake(0, 0, 30, 30)]; 

    [button1 addTarget:self action:@selector(ScreenImage) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button1];

 


3.响应函数


-(void)ScreenImage{ 

    ///调取脚本函数

    UnitySendMessage("ARCamera","ScreenImage",""); 

   

 //保存到相册

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

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

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"]; 

    UIImage *image = [UIImage imageWithContentsOfFile:filePath2]; 

    UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);//保存

}

IPhone获取文件路径四种方法
1,document 中的路径  最麻烦的一种:

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

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:filename;   

 

 本人自写自用的宏

2,#define ABS_FILE_PATH(FILE_NAME) [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),FILE_NAME]

 

 

3,获取程序application package目录下的文件路径:

#define PACKAGE_FILE_PATH(FILE_NAME) [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FILE_NAME]

 

4,NSString* filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"fileType(txt...html..)"];

 

IOS图片截取
第一种:整个屏幕截取

在ViewController中写的,这个类是一个视图控制器


-(void)loadView{

   //静态方法sharedApplication
    [[UIApplication sharedApplication]setStatusBarHidden:YES //把状态栏隐藏
                                           withAnimation:UIStatusBarAnimationSlide];
    UIImage *mage=[UIImage imageNamed:@"image.png"];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];

    //UIImageView *im=[UIImageView alloc]initWithImage:mage];
    [imageView setImage:mage];
   
    self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
               [self.view addSubview:contentView];
    CGRect rect=CGRectMake(0, 0, 320, 480);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef currentContext=UIGraphicsGetCurrentContext();
    CGContextClipToRect(currentContext,rect);
    CGContextDrawImage(currentContext, rect, mage.CGImage);
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    contentView.image=image;
    self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    [self.view addSubview:contentView];
    [image release];
   
}
第二种:整张图片的缩略

-(void)loadView{
    [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    UIImage *image=[UIImage imageNamed:@"image.png"];
   //UIImageView *contentView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    //用来控制图片缩到多大
    CGRect rect=CGRectMake(0, 0, 160, 240);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef currentContent=UIGraphicsGetCurrentContext();
    CGContextClipToRect(currentContent, rect);
    [image drawInRect:rect];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *contentView=[[UIImageView alloc]initWithFrame:rect];
    contentView.image=image;
    self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    [self.view addSubview:contentView];
   
    [image release];
}


第san种:真正的截取图片

- (void)loadView {
  
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
    UIImage *image=[UIImage imageNamed:@"image2.png"];
   
    CGRect rect = CGRectMake(33, 22, 140, 353);//创建矩形框
    CGRect re=CGRectMake(0, 0, 140, 140);
    UIImageView *contentView = [[UIImageView alloc] initWithFrame:re];
    contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect)];
   
    self.view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    ImageView *imageView=[[ImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    self.view.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:imageView];
    [self.view addSubview:contentView];
   
    [image release];
}

******************************以下是写的一个简单的Demo******************************

#import <UIKit/UIKit.h>

@interface imageView : UIView{
    CGPoint starPoint;
    CGPoint endPoint;
    UIImage *image;

}

@end

#import "imageView.h"

@implementation imageView


-(void)dealloc{
   
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        allMovePoints=[[NSMutableArray alloc]init];
        //self.frame=CGRectMake(0, 0, 220, 340);
        self.frame=CGRectMake(0, 0, 320, 480);
        image=[UIImage imageNamed:@"image.png"];
        self.backgroundColor=[UIColor colorWithPatternImage:image];
//        UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
//        imageView.tag=3;
//        imageView.frame=CGRectMake(0, 0, 320, 360);
//        [self addSubview:imageView];
       
        // Initialization code
    }
    return self;
}
-(void)drawRect:(CGRect)rect{
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1, 5.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 2.0);
    CGContextAddRect(context, CGRectMake(starPoint.x, starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y));
    CGContextStrokePath(context);
   

   
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *t=[touches anyObject];
    starPoint=[t locationInView:self];

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *t=[touches anyObject];
    //CGPoint p=[t locationInView:self];
    endPoint=[t locationInView:self];
    UIImageView *image11=(UIImageView *)[self viewWithTag:3];
    [image11 removeFromSuperview];
    //[allMovePoints addObject:NSStringFromCGPoint(p)];
    [self setNeedsDisplay];
   
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGRect rect1 = CGRectMake(starPoint.x, starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y);//创建矩形框
    CGRect re=CGRectMake(320-endPoint.x+starPoint.x, 480-endPoint.y+starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y);
    UIImageView *contentView = [[UIImageView alloc] initWithFrame:re];
    contentView.tag=3;
    contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect1)];
    [self addSubview:contentView];
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值