File and folder Problems

Source: Internet
Author: User

IOS obtains the Document folder path


NSFileManager * fileManager = [NSFileManager defaultManager];

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

NSString * documentsDirectory = [paths objectAtIndex: 0];

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

IOS camera retrieval and photo taking
<UIImagePickerControllerDelegate>


1. Open the camera

-(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. Photo callback


-(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 isw.tostring: @ "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 isw.tostring: @ "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 File Download (synchronous download)


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];

/* Downloaded data */

If (data! = Nil ){

NSLog (@ "Download successful ");

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

NSLog (@ "saved successfully .");

}

Else

{

NSLog (@ "failed to save .");

}

} Else {

NSLog (@ "% @", error );

}

IOS vibration effect
AudioToolBox. framework

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

Get the device number for IOS


UIDevice * device = [UIDevice currentDevice]; // create a device object

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

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

[View show];

Unity 3D screenshot

1. js script (added to ARCamera)


# Pragma strict

Function Start (){}

Function Update (){}

Function ScreenImage (){

Application. CaptureScreenshot ("Screenshot.png ");

}

2. Add 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. Response Functions


-(Void) ScreenImage {

/// Retrieve the Script Function

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

// Save to album

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); // save

}

Four methods for obtaining file paths for IPhone
1. Path in document is the most troublesome one:

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

NSString * documentsDirectory = [paths objectAtIndex: 0];

NSString * writableDBPath = [documentsDirectory stringByAppendingPathComponent: filename;

 

Self-built macros

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

 

 

3. Obtain the file path under the application package directory:

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

 

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

 

IOS screenshot
First: capture the entire Screen

Written in ViewController. This class is a view controller.


-(Void) loadView {

// Static sharedApplication
[[UIApplication sharedApplication] setStatusBarHidden: YES // hide the status bar
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];

}
Type 2: scaling down the entire image

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

// Used to control the scale of an image
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];
}


Level 3: truly captured images

-(Void) loadView {
 
[[UIApplication sharedApplication] setStatusBarHidden: YES withAnimation: UIStatusBarAnimationSlide];
UIImage * image = [UIImage imageNamed: @ "image2.png"];

CGRect rect = CGRectMake (33, 22,140,353); // create a rectangle
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];
}

* ***************************** The following is a simple Demo for writing ** ****************************

# 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); // create a Rectangular Box
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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.