こんにちは、ほとんどが機能するコードがいくつかあります。基本的に、配列からの画像の数をフェード効果でアニメーション化します。
基本的にはすべてうまくいきますが、ビューが最初に配列内の最初の画像をロードしたときに、正しい位置に90度回転してから同じ画像にフェードするように見えますが、このようなアニメーションを見るのは面倒です。上記のバグだけで、画像から別の画像へのフェードはすべて完璧です。そして、前述のように、最初のロード時だけです。
これがコードです。および両方でimages
あり、両方ともivarであり、**合成された**です。配列も同様です。MutableArray
topImageView
bottomImageView
images
int topIndex = 0, bottomIndex = 1;
-(void)imageFader{
self.imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];
[self.view addSubview:imageViewBottom];
[imageViewBottom setAlpha:0.0];
[imageViewBottom release];
self.imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];
[self.view addSubview:imageViewTop];
[imageViewTop setAlpha:1.0];
[imageViewTop release];
[imageViewTop setImage:[images objectAtIndex:topIndex]];
timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
}
-(void)onTimer{
[UIView animateWithDuration:1 animations:^{
//set the image of the image that is not currently visible
if (imageViewTop.alpha == 0) {
[imageViewTop setImage:[images objectAtIndex:topIndex]];
}
if (imageViewBottom.alpha == 0) {
[imageViewBottom setImage:[images objectAtIndex:bottomIndex]];
}
//make sure the images rotate
[imageViewTop setAlpha:imageViewTop.alpha == 0 ? 1 : 0];
[imageViewBottom setAlpha:imageViewBottom.alpha == 0 ? 1 : 0];
//make sure the images play in a loop
topIndex = topIndex < [images count]-1 ? bottomIndex+1 : 0;
bottomIndex = bottomIndex < [images count]-1 ? bottomIndex+1 : 0;}];
}
MutableArray
アニメーション用にロードする前に、画像を docs ディレクトリに保存する方法を次に示します。
-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
//Save to camera roll
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) ALAssetOrientationRight completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"completionBlock");
}];
int imageNumber = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathToFile;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
do
{
// increment the image
imageNumber++;
// get the new path to the file
pathToFile = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:
@"standingImage%d.JPG", imageNumber]];
}
while([fileManager fileExistsAtPath:pathToFile]);
/* so, we loop for as long as we keep coming up with names that already exist */
UIImage *image2 = image; // imageView is my image from camera
//Use UIImageJPEGRepresentation to maitain image orientation!
NSData *imageData = UIImageJPEGRepresentation(image2, 0.9);
[imageData writeToFile:pathToFile atomically:NO];
[self dismissModalViewControllerAnimated:YES];