你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> 文件,文件夾問題

文件,文件夾問題

編輯:關於IOS

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

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved